hard edges rework, add hard edge in gwdw, config.ac nv/cuda tweaks, message log warn...
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / sema.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "bcsignals.h"
23 #include "sema.h"
24
25
26
27
28 Sema::Sema(int init_value, const char *title)
29 {
30         sem_init(&sem, 0, init_value);
31         this->title = title;
32         this->init_value = init_value;
33 }
34
35 Sema::~Sema()
36 {
37         sem_destroy(&sem);
38         UNSET_ALL_LOCKS(this);
39 }
40
41
42 void Sema::lock(const char *location)
43 {
44         SET_LOCK(this, title, location);
45         sem_wait(&sem);
46         SET_LOCK2
47 }
48
49 void Sema::unlock()
50 {
51         UNSET_LOCK(this);
52         sem_post(&sem);
53 }
54
55 int Sema::get_value()
56 {
57         int result;
58         sem_getvalue(&sem, &result);
59         return result;
60 }
61
62 void Sema::reset()
63 {
64         sem_destroy(&sem);
65         sem_init(&sem, 0, init_value);
66         UNSET_ALL_LOCKS(this)
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 #if 0
88
89 #include "bcipc.h"
90 #include "sema.h"
91
92 Sema::Sema(int id, int number)
93 {
94         if(id == -1)
95         {
96                 if((semid = semget(IPC_PRIVATE, number, IPC_CREAT | 0777)) < 0) perror("Sema::Sema");
97                 for(int i = 0; i < number; i++) unlock(i);
98                 client = 0;
99                 bc_enter_sema_id(semid);
100         }
101         else
102         {
103                 client = 1;
104                 this->semid = id;
105         }
106
107         semas = number;
108 }
109
110 Sema::~Sema()
111 {
112         if(!client)
113         {
114                 if(semctl(semid, 0, IPC_RMID, arg) < 0) perror("Sema::~Sema");
115                 bc_remove_sema_id(semid);
116         }
117 }
118
119
120 int Sema::lock(int number)
121 {
122         struct sembuf sop;
123
124 // decrease the semaphore
125         sop.sem_num = number;
126         sop.sem_op = -1;
127         sop.sem_flg = 0;
128         if(semop(semid, &sop, 1) < 0) perror("Sema::lock");
129         return 0;
130 }
131
132 int Sema::unlock(int number)
133 {
134         struct sembuf sop;
135
136 // decrease the semaphore
137         sop.sem_num = number;
138         sop.sem_op = 1;
139         sop.sem_flg = 0;
140         if(semop(semid, &sop, 1) < 0) perror("Sema::unlock");
141         return 0;
142 }
143
144 int Sema::get_value(int number)
145 {
146         return semctl(semid, number, GETVAL, arg);
147 }
148
149 int Sema::get_id()
150 {
151         return semid;
152 }
153
154
155 #endif