Subversion Repositories HomeAutomation

Rev

Rev 270 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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