root/trunk/whisperlib/common/sync/thread.h

Revision 7, 3.0 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Catalin Popescu
31
32 #ifndef __COMMON_SYNC_THREAD_H__
33 #define __COMMON_SYNC_THREAD_H__
34
35 #include <pthread.h>
36 #include <limits.h>
37 #include <whisperlib/common/base/log.h>
38 #include <whisperlib/common/base/callback.h>
39
40 namespace thread {
41
42 class Thread {
43  public:
44   // Create a thread that will run the given function
45   Thread();
46   explicit Thread(Closure* thread_function);
47   ~Thread();
48
49   // Actually spawns the thread
50   bool Start();
51   // Waits for the thread to finish
52   bool Join();
53   // Enables waiting on this thread (joining)
54   bool SetJoinable();
55   // Set the stack size for this thread.
56   // Min value is: PTHREAD_STACK_MIN (=16384 in /usr/include/bits/local_lim.h)
57   // Max value is: system-imposed limit
58   // Default value is system dependent:
59   //
60   //  Architecture #CPUs RAM(GB) DefaultSize (bytes)
61   //  ------------------------------------------------
62   //  AMD Opteron  8     16      2,097,152
63   //  Intel IA64   4     8       33,554,432
64   //  Intel IA32   2     4       2,097,152
65   //  IBM Power5   8     32      196,608
66   //  IBM Power4   8     16      196,608
67   //  IBM Power3  16     16      98,304
68   //
69   bool SetStackSize(size_t stacksize);
70   // Stops the thread unconditionally
71   bool Kill();
72
73   void set_thread_function(Closure* closure) {
74     thread_function_ = closure;
75   }
76
77  protected:
78   pthread_t thread_;
79   pthread_attr_t attr_;
80   Closure* thread_function_;
81  private:
82   DISALLOW_EVIL_CONSTRUCTORS(Thread);
83 };
84 }
85
86 #endif  // __COMMON_SYNC_THREAD_H__
Note: See TracBrowser for help on using the browser.