Rev 1398 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 270 | johboh | 1 | /*************************************************************************** |
| 2 | rlthread.cpp - description |
||
| 3 | ------------------- |
||
| 4 | begin : Tue Jan 02 2001 |
||
| 5 | copyright : (C) 2001 by Rainer Lehrig |
||
| 6 | email : lehrig@t-online.de |
||
| 7 | ***************************************************************************/ |
||
| 8 | |||
| 9 | /*************************************************************************** |
||
| 10 | * * |
||
| 11 | * This library is free software; you can redistribute it and/or modify * |
||
| 12 | * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as * |
||
| 13 | * published by the Free Software Foundation * |
||
| 14 | * * |
||
| 15 | ***************************************************************************/ |
||
| 16 | #include "rlthread.h" |
||
| 17 | |||
| 18 | rlThread::rlThread(int max_semaphore) |
||
| 19 | { |
||
| 20 | rlwthread_attr_init(&attr); |
||
| 21 | arg.thread = this; |
||
| 22 | arg.user = NULL; |
||
| 23 | rlwthread_mutex_init(&mutex, NULL); |
||
| 24 | rlwrapinit_semaphore(&semaphore, max_semaphore); |
||
| 25 | arg.running = 0; |
||
| 26 | } |
||
| 27 | |||
| 28 | rlThread::~rlThread() |
||
| 29 | { |
||
| 30 | arg.running = 0; |
||
| 31 | rlwthread_mutex_destroy(&mutex); |
||
| 32 | rlwrapdestroy_semaphore(&semaphore); |
||
| 33 | } |
||
| 34 | |||
| 35 | int rlThread::create(void *(*func)(void*), void *user) |
||
| 36 | { |
||
| 37 | arg.user = user; |
||
| 38 | arg.running = 1; |
||
| 39 | return rlwthread_create(&tid, &attr, func, &arg); |
||
| 40 | } |
||
| 41 | |||
| 42 | int rlThread::trylock() |
||
| 43 | { |
||
| 44 | return rlwthread_mutex_trylock(&mutex); |
||
| 45 | } |
||
| 46 | |||
| 47 | int rlThread::lock() |
||
| 48 | { |
||
| 49 | return rlwthread_mutex_lock(&mutex); |
||
| 50 | } |
||
| 51 | |||
| 52 | int rlThread::unlock() |
||
| 53 | { |
||
| 54 | return rlwthread_mutex_unlock(&mutex); |
||
| 55 | } |
||
| 56 | |||
| 57 | int rlThread::waitSemaphore() |
||
| 58 | { |
||
| 59 | return rlwrapwait_semaphore(&semaphore); |
||
| 60 | } |
||
| 61 | |||
| 62 | int rlThread::incrementSemaphore() |
||
| 63 | { |
||
| 64 | return rlwrapincrement_semaphore(&semaphore); |
||
| 65 | } |
||
| 66 | |||
| 67 | void rlThread::threadExit(void *status) |
||
| 68 | { |
||
| 69 | arg.running = 0; |
||
| 70 | rlwthread_exit(status); |
||
| 71 | } |
||
| 72 | |||
| 73 | int rlThread::join(void **status) |
||
| 74 | { |
||
| 75 | return rlwthread_join(tid, status); |
||
| 76 | } |
||
| 77 | |||
| 78 | int rlThread::cancel() |
||
| 79 | { |
||
| 80 | arg.running = 0; |
||
| 81 | return rlwthread_cancel(tid); |
||
| 82 | } |
||
| 83 | |||
| 84 | //------------ class rlMutex ---------------------------------------------- |
||
| 85 | rlMutex::rlMutex(const pthread_mutexattr_t *attr) |
||
| 86 | { |
||
| 87 | rlwthread_mutex_init(&mutex, attr); |
||
| 88 | } |
||
| 89 | |||
| 90 | rlMutex::~rlMutex() |
||
| 91 | { |
||
| 92 | rlwthread_mutex_destroy(&mutex); |
||
| 93 | } |
||
| 94 | |||
| 95 | int rlMutex::trylock() |
||
| 96 | { |
||
| 97 | return rlwthread_mutex_trylock(&mutex); |
||
| 98 | } |
||
| 99 | |||
| 100 | int rlMutex::lock() |
||
| 101 | { |
||
| 102 | return rlwthread_mutex_lock(&mutex); |
||
| 103 | } |
||
| 104 | |||
| 105 | int rlMutex::unlock() |
||
| 106 | { |
||
| 107 | return rlwthread_mutex_unlock(&mutex); |
||
| 108 | } |
||
| 109 | |||
| 110 | //------------ class rlSemaphore ------------------------------------------ |
||
| 111 | rlSemaphore::rlSemaphore(int max_semaphore) |
||
| 112 | { |
||
| 113 | rlwrapinit_semaphore(&semaphore, max_semaphore); |
||
| 114 | } |
||
| 115 | |||
| 116 | rlSemaphore::~rlSemaphore() |
||
| 117 | { |
||
| 118 | rlwrapdestroy_semaphore(&semaphore); |
||
| 119 | } |
||
| 120 | |||
| 121 | int rlSemaphore::waitSemaphore() |
||
| 122 | { |
||
| 123 | return rlwrapwait_semaphore(&semaphore); |
||
| 124 | } |
||
| 125 | |||
| 126 | int rlSemaphore::incrementSemaphore() |
||
| 127 | { |
||
| 128 | return rlwrapincrement_semaphore(&semaphore); |
||
| 129 | } |