进程间同步

/*****************************************************************************/
202
203/*
204 * Simple mutex class.  The implementation is system-dependent.
205 *
206 * The mutex must be unlocked by the thread that locked it.  They are not
207 * recursive, i.e. the same thread can't lock it multiple times.
208 */
209class Mutex {
210public:
211    enum {
212        PRIVATE = 0,
213        SHARED = 1
214    };
215
216                Mutex();
217                Mutex(const char* name);
218                Mutex(int type, const char* name = NULL);
219                ~Mutex();
220
221    // lock or unlock the mutex
222    status_t    lock();
223    void        unlock();
224
225    // lock if possible; returns 0 on success, error otherwise
226    status_t    tryLock();
227
228    // Manages the mutex automatically. It'll be locked when Autolock is
229    // constructed and released when Autolock goes out of scope.
230    class Autolock {
231    public:
232        inline Autolock(Mutex& mutex) : mLock(mutex)  { mLock.lock(); }
233        inline Autolock(Mutex* mutex) : mLock(*mutex) { mLock.lock(); }
234        inline ~Autolock() { mLock.unlock(); }
235    private:
236        Mutex& mLock;
237    };
238
239private:
240    friend class Condition;
241
242    // A mutex cannot be copied
243                Mutex(const Mutex&);
244    Mutex&      operator = (const Mutex&);
245
246#if defined(HAVE_PTHREADS)
247    pthread_mutex_t mMutex;
248#else
249    void    _init();
250    void*   mState;
251#endif
252};
253
254#if defined(HAVE_PTHREADS)
255
256inline Mutex::Mutex() {
257    pthread_mutex_init(&mMutex, NULL);
258}
259inline Mutex::Mutex(const char* name) {
260    pthread_mutex_init(&mMutex, NULL);
261}
262inline Mutex::Mutex(int type, const char* name) {
263    if (type == SHARED) {
264        pthread_mutexattr_t attr;
265        pthread_mutexattr_init(&attr);
266        pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
267        pthread_mutex_init(&mMutex, &attr);
268        pthread_mutexattr_destroy(&attr);
269    } else {
270        pthread_mutex_init(&mMutex, NULL);
271    }
272}
273inline Mutex::~Mutex() {
274    pthread_mutex_destroy(&mMutex);
275}
276inline status_t Mutex::lock() {
277    return -pthread_mutex_lock(&mMutex);
278}
279inline void Mutex::unlock() {
280    pthread_mutex_unlock(&mMutex);
281}
282inline status_t Mutex::tryLock() {
283    return -pthread_mutex_trylock(&mMutex);
284}
285
286#endif // HAVE_PTHREADS
287
288/*
289 * Automatic mutex.  Declare one of these at the top of a function.
290 * When the function returns, it will go out of scope, and release the
291 * mutex.
292 */
293
294typedef Mutex::Autolock AutoMutex;
295
296/*****************************************************************************/
297
298#if defined(HAVE_PTHREADS)
299
300/*
301 * Simple mutex class.  The implementation is system-dependent.
302 *
303 * The mutex must be unlocked by the thread that locked it.  They are not
304 * recursive, i.e. the same thread can't lock it multiple times.
305 */
306class RWLock {
307public:
308    enum {
309        PRIVATE = 0,
310        SHARED = 1
311    };
312
313                RWLock();
314                RWLock(const char* name);
315                RWLock(int type, const char* name = NULL);
316                ~RWLock();
317
318    status_t    readLock();
319    status_t    tryReadLock();
320    status_t    writeLock();
321    status_t    tryWriteLock();
322    void        unlock();
323
324    class AutoRLock {
325    public:
326        inline AutoRLock(RWLock& rwlock) : mLock(rwlock)  { mLock.readLock(); }
327        inline ~AutoRLock() { mLock.unlock(); }
328    private:
329        RWLock& mLock;
330    };
331
332    class AutoWLock {
333    public:
334        inline AutoWLock(RWLock& rwlock) : mLock(rwlock)  { mLock.writeLock(); }
335        inline ~AutoWLock() { mLock.unlock(); }
336    private:
337        RWLock& mLock;
338    };
339
340private:
341    // A RWLock cannot be copied
342                RWLock(const RWLock&);
343   RWLock&      operator = (const RWLock&);
344
345   pthread_rwlock_t mRWLock;
346};
347
348inline RWLock::RWLock() {
349    pthread_rwlock_init(&mRWLock, NULL);
350}
351inline RWLock::RWLock(const char* name) {
352    pthread_rwlock_init(&mRWLock, NULL);
353}
354inline RWLock::RWLock(int type, const char* name) {
355    if (type == SHARED) {
356        pthread_rwlockattr_t attr;
357        pthread_rwlockattr_init(&attr);
358        pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
359        pthread_rwlock_init(&mRWLock, &attr);
360        pthread_rwlockattr_destroy(&attr);
361    } else {
362        pthread_rwlock_init(&mRWLock, NULL);
363    }
364}
365inline RWLock::~RWLock() {
366    pthread_rwlock_destroy(&mRWLock);
367}
368inline status_t RWLock::readLock() {
369    return -pthread_rwlock_rdlock(&mRWLock);
370}
371inline status_t RWLock::tryReadLock() {
372    return -pthread_rwlock_tryrdlock(&mRWLock);
373}
374inline status_t RWLock::writeLock() {
375    return -pthread_rwlock_wrlock(&mRWLock);
376}
377inline status_t RWLock::tryWriteLock() {
378    return -pthread_rwlock_trywrlock(&mRWLock);
379}
380inline void RWLock::unlock() {
381    pthread_rwlock_unlock(&mRWLock);
382}
383
384#endif // HAVE_PTHREADS
385
386/*****************************************************************************/
387
388/*
389 * Condition variable class.  The implementation is system-dependent.
390 *
391 * Condition variables are paired up with mutexes.  Lock the mutex,
392 * call wait(), then either re-wait() if things aren't quite what you want,
393 * or unlock the mutex and continue.  All threads calling wait() must
394 * use the same mutex for a given Condition.
395 */
396class Condition {
397public:
398    enum {
399        PRIVATE = 0,
400        SHARED = 1
401    };
402
403    Condition();
404    Condition(int type);
405    ~Condition();
406    // Wait on the condition variable.  Lock the mutex before calling.
407    status_t wait(Mutex& mutex);
408    // same with relative timeout
409    status_t waitRelative(Mutex& mutex, nsecs_t reltime);
410    // Signal the condition variable, allowing one thread to continue.
411    void signal();
412    // Signal the condition variable, allowing all threads to continue.
413    void broadcast();
414
415private:
416#if defined(HAVE_PTHREADS)
417    pthread_cond_t mCond;
418#else
419    void*   mState;
420#endif
421};
422
423#if defined(HAVE_PTHREADS)
424
425inline Condition::Condition() {
426    pthread_cond_init(&mCond, NULL);
427}
428inline Condition::Condition(int type) {
429    if (type == SHARED) {
430        pthread_condattr_t attr;
431        pthread_condattr_init(&attr);
432        pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
433        pthread_cond_init(&mCond, &attr);
434        pthread_condattr_destroy(&attr);
435    } else {
436        pthread_cond_init(&mCond, NULL);
437    }
438}
439inline Condition::~Condition() {
440    pthread_cond_destroy(&mCond);
441}
442inline status_t Condition::wait(Mutex& mutex) {
443    return -pthread_cond_wait(&mCond, &mutex.mMutex);
444}
445inline status_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime) {
446#if defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE)
447    struct timespec ts;
448    ts.tv_sec  = reltime/1000000000;
449    ts.tv_nsec = reltime%1000000000;
450    return -pthread_cond_timedwait_relative_np(&mCond, &mutex.mMutex, &ts);
451#else // HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE
452    struct timespec ts;
453#if defined(HAVE_POSIX_CLOCKS)
454    clock_gettime(CLOCK_REALTIME, &ts);
455#else // HAVE_POSIX_CLOCKS
456    // we don't support the clocks here.
457    struct timeval t;
458    gettimeofday(&t, NULL);
459    ts.tv_sec = t.tv_sec;
460    ts.tv_nsec= t.tv_usec*1000;
461#endif // HAVE_POSIX_CLOCKS
462    ts.tv_sec += reltime/1000000000;
463    ts.tv_nsec+= reltime%1000000000;
464    if (ts.tv_nsec >= 1000000000) {
465        ts.tv_nsec -= 1000000000;
466        ts.tv_sec  += 1;
467    }
468    return -pthread_cond_timedwait(&mCond, &mutex.mMutex, &ts);
469#endif // HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE
470}
471inline void Condition::signal() {
472    pthread_cond_signal(&mCond);
473}
474inline void Condition::broadcast() {
475    pthread_cond_broadcast(&mCond);
476}
477
478#endif // HAVE_PTHREADS
479
480/*****************************************************************************/

使用:

1.把Mutex,RWLock,Condition放在共享内存中
2.设置他们的共享属性

1.映射共享内存,并做如下的初始化创建工作:
Mutex lock(Mutex::SHARED);  
Condition cv(Condition::SHARED);
RWLock mLock(RWLock::SHARED);

2.接着他们就可以拿来跨进程同步了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值