rllib  1
rlthread.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           rlthread.cpp  -  description
00003                              -------------------
00004     begin                : Tue Jan 02 2001
00005     copyright            : (C) 2001 by Rainer Lehrig
00006     email                : lehrig@t-online.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This library is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as        *
00013  *   published by the Free Software Foundation                             *
00014  *                                                                         *
00015  ***************************************************************************/
00016 #include "rlthread.h"
00017 
00018 rlThread::rlThread(int max_semaphore)
00019 {
00020   rlwthread_attr_init(&attr);
00021   arg.thread = this;
00022   arg.user   = NULL;
00023   rlwthread_mutex_init(&mutex, NULL);
00024   rlwrapinit_semaphore(&semaphore, max_semaphore);
00025   arg.running = 0;
00026 }
00027 
00028 rlThread::~rlThread()
00029 {
00030   arg.running = 0;
00031   rlwthread_mutex_destroy(&mutex);
00032   rlwrapdestroy_semaphore(&semaphore);
00033 }
00034 
00035 int rlThread::create(void *(*func)(void*), void *user)
00036 {
00037   arg.user = user;
00038   arg.running = 1;
00039   return rlwthread_create(&tid, &attr, func, &arg);
00040 }
00041 
00042 int rlThread::trylock()
00043 {
00044   return rlwthread_mutex_trylock(&mutex);
00045 }
00046 
00047 int rlThread::lock()
00048 {
00049   return rlwthread_mutex_lock(&mutex);
00050 }
00051 
00052 int rlThread::unlock()
00053 {
00054   return rlwthread_mutex_unlock(&mutex);
00055 }
00056 
00057 int rlThread::waitSemaphore()
00058 {
00059   return rlwrapwait_semaphore(&semaphore);
00060 }
00061 
00062 int rlThread::incrementSemaphore()
00063 {
00064   return rlwrapincrement_semaphore(&semaphore);
00065 }
00066 
00067 void rlThread::threadExit(void *status)
00068 {
00069   arg.running = 0;
00070   rlwthread_exit(status);
00071 }
00072 
00073 int rlThread::join(void **status)
00074 {
00075   return rlwthread_join(tid, status);
00076 }
00077 
00078 int rlThread::cancel()
00079 {
00080   arg.running = 0;
00081   return rlwthread_cancel(tid);
00082 }
00083 
00084 //------------ class rlMutex ----------------------------------------------
00085 rlMutex::rlMutex(const pthread_mutexattr_t *attr)
00086 {
00087   rlwthread_mutex_init(&mutex, attr);
00088 }
00089 
00090 rlMutex::~rlMutex()
00091 {
00092   rlwthread_mutex_destroy(&mutex);
00093 }
00094 
00095 int rlMutex::trylock()
00096 {
00097   return rlwthread_mutex_trylock(&mutex);
00098 }
00099 
00100 int rlMutex::lock()
00101 {
00102   return rlwthread_mutex_lock(&mutex);
00103 }
00104 
00105 int rlMutex::unlock()
00106 {
00107   return rlwthread_mutex_unlock(&mutex);
00108 }
00109 
00110 //------------ class rlSemaphore ------------------------------------------
00111 rlSemaphore::rlSemaphore(int max_semaphore)
00112 {
00113   rlwrapinit_semaphore(&semaphore, max_semaphore);
00114 }
00115 
00116 rlSemaphore::~rlSemaphore()
00117 {
00118   rlwrapdestroy_semaphore(&semaphore);
00119 }
00120 
00121 int rlSemaphore::waitSemaphore()
00122 {
00123   return rlwrapwait_semaphore(&semaphore);
00124 }
00125 
00126 int rlSemaphore::incrementSemaphore()
00127 {
00128   return rlwrapincrement_semaphore(&semaphore);
00129 }