| 1 | /* |
|---|
| 2 | * Copyright (c) 2005, Eric Crahen |
|---|
| 3 | * |
|---|
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
|---|
| 5 | * of this software and associated documentation files (the "Software"), to deal |
|---|
| 6 | * in the Software without restriction, including without limitation the rights |
|---|
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|---|
| 8 | * copies of the Software, and to permit persons to whom the Software is furnished |
|---|
| 9 | * to do so, subject to the following conditions: |
|---|
| 10 | * |
|---|
| 11 | * The above copyright notice and this permission notice shall be included in all |
|---|
| 12 | * copies or substantial portions of the Software. |
|---|
| 13 | * |
|---|
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|---|
| 18 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
|---|
| 19 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #ifndef __ZTTHREADOPS_H__ |
|---|
| 24 | #define __ZTTHREADOPS_H__ |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | #include "zthread/Priority.h" |
|---|
| 28 | #include <pthread.h> |
|---|
| 29 | #include <assert.h> |
|---|
| 30 | |
|---|
| 31 | namespace ZThread { |
|---|
| 32 | |
|---|
| 33 | class Runnable; |
|---|
| 34 | |
|---|
| 35 | //! Dispatch function for native pthreads required extern C |
|---|
| 36 | //! linkage. |
|---|
| 37 | extern "C" void* _dispatch(void*); |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * @class ThreadOps |
|---|
| 41 | * @author Eric Crahen <http://www.code-foo.com> |
|---|
| 42 | * @date <2003-07-16T23:30:25-0400> |
|---|
| 43 | * @version 2.2.8 |
|---|
| 44 | * |
|---|
| 45 | * This class is an abstraction used to perform various operations on a |
|---|
| 46 | * native POSIX thread. |
|---|
| 47 | */ |
|---|
| 48 | class ThreadOps { |
|---|
| 49 | |
|---|
| 50 | //! Keep track of the pthreads handle for the native thread |
|---|
| 51 | pthread_t _tid; |
|---|
| 52 | |
|---|
| 53 | ThreadOps(pthread_t tid) : _tid(tid) { } |
|---|
| 54 | |
|---|
| 55 | public: |
|---|
| 56 | |
|---|
| 57 | const static ThreadOps INVALID; |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | * Create a new ThreadOps to manipulate a native thread. |
|---|
| 61 | */ |
|---|
| 62 | ThreadOps() : _tid(0) { } |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | inline bool operator==(const ThreadOps& ops) const { |
|---|
| 66 | return pthread_equal(_tid, ops._tid); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | static ThreadOps self() { |
|---|
| 71 | return ThreadOps(pthread_self()); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | /** |
|---|
| 75 | * Activating an instance of ThreadOps will map it onto the currently |
|---|
| 76 | * executing thread. |
|---|
| 77 | */ |
|---|
| 78 | static void activate(ThreadOps* ops) { |
|---|
| 79 | |
|---|
| 80 | assert(ops); |
|---|
| 81 | assert(ops->_tid == 0); |
|---|
| 82 | |
|---|
| 83 | ops->_tid = pthread_self(); |
|---|
| 84 | |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | /** |
|---|
| 88 | * Test if this object represents the currently executing |
|---|
| 89 | * native thread. |
|---|
| 90 | * |
|---|
| 91 | * @return bool true if successful |
|---|
| 92 | */ |
|---|
| 93 | |
|---|
| 94 | static bool isCurrent(ThreadOps* ops) { |
|---|
| 95 | |
|---|
| 96 | assert(ops); |
|---|
| 97 | |
|---|
| 98 | return pthread_equal(pthread_self(), ops->_tid); |
|---|
| 99 | |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | /** |
|---|
| 103 | * Join a native thread. |
|---|
| 104 | * |
|---|
| 105 | * @return bool true if successful |
|---|
| 106 | */ |
|---|
| 107 | static bool join(ThreadOps*); |
|---|
| 108 | |
|---|
| 109 | /** |
|---|
| 110 | * Force the current native thread to yield, letting the scheduler |
|---|
| 111 | * give the CPU time to another thread. |
|---|
| 112 | * |
|---|
| 113 | * @return bool true if successful, false if the operation can't |
|---|
| 114 | * be supported. |
|---|
| 115 | */ |
|---|
| 116 | static bool yield(); |
|---|
| 117 | |
|---|
| 118 | /** |
|---|
| 119 | * Set the priority for the native thread if supported by the |
|---|
| 120 | * system. |
|---|
| 121 | * |
|---|
| 122 | * @param PRIORITY requested priority |
|---|
| 123 | * @return bool false if unsuccessful |
|---|
| 124 | */ |
|---|
| 125 | static bool setPriority(ThreadOps*, Priority); |
|---|
| 126 | |
|---|
| 127 | /** |
|---|
| 128 | * Set the priority for the native thread if supported by the |
|---|
| 129 | * system. |
|---|
| 130 | * |
|---|
| 131 | * @param Thread::PRIORITY& current priority |
|---|
| 132 | * @return bool false if unsuccessful |
|---|
| 133 | */ |
|---|
| 134 | static bool getPriority(ThreadOps*, Priority&); |
|---|
| 135 | |
|---|
| 136 | protected: |
|---|
| 137 | |
|---|
| 138 | /** |
|---|
| 139 | * Spawn a native thread. |
|---|
| 140 | * |
|---|
| 141 | * @param ThreadImpl* parent thread |
|---|
| 142 | * @param ThreadImpl* child thread being started. |
|---|
| 143 | * @param Runnable* task being executed. |
|---|
| 144 | * |
|---|
| 145 | * @return bool true if successful |
|---|
| 146 | */ |
|---|
| 147 | bool spawn(Runnable*); |
|---|
| 148 | |
|---|
| 149 | }; |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | #endif // __ZTTHREADOPS_H__ |
|---|