4
Copyright (C) 2007 by Steven Yi
6
Mutex lock and unlocking opcodes, used for when running Csound with
9
This file is part of Csound.
11
The Csound Library is free software; you can redistribute it
12
and/or modify it under the terms of the GNU Lesser General Public
13
License as published by the Free Software Foundation; either
14
version 2.1 of the License, or (at your option) any later version.
16
Csound is distributed in the hope that it will be useful,
17
but WITHOUT ANY WARRANTY; without even the implied warranty of
18
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
GNU Lesser General Public License for more details.
21
You should have received a copy of the GNU Lesser General Public
22
License along with Csound; if not, write to the Free Software
23
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
30
static std::map<CSOUND *, std::map<size_t, std::map<size_t, void *> > > mutexes;
38
static void * getMutex(CSOUND *csound, size_t instrNum, size_t lockNum)
41
csound_global_mutex_lock();
42
if(mutexes.find(csound) == mutexes.end()) {
43
std::map<size_t, std::map<size_t, void *> > values;
44
mutexes[csound] = values;
47
if(mutexes[csound].find(instrNum) == mutexes[csound].end()) {
48
std::map<size_t, void*> mutexMap;
49
mutexes[csound][instrNum] = mutexMap;
52
if (mutexes[csound][instrNum].find(lockNum) ==
53
mutexes[csound][instrNum].end()) {
54
void * mutex = csound->Create_Mutex(0);
55
mutexes[csound][instrNum][lockNum] = mutex;
57
csound->Message(csound, "Created new mutex [%ld:%ld]\n",
58
(long)instrNum, (long)lockNum);
60
csound_global_mutex_unlock();
62
return mutexes[csound][instrNum][lockNum];
66
static int mutexLock(CSOUND *csound, OPCODE_MUTEX *p)
69
if(p->mutex == NULL) {
70
size_t instrNum = static_cast<size_t>(p->h.insdshead->p1);
71
size_t lockNum = static_cast<size_t>(*p->ilocknum);
73
p->mutex = getMutex(csound, instrNum, lockNum);
76
csound->LockMutex(p->mutex);
81
static int mutexUnlock(CSOUND *csound, OPCODE_MUTEX *p)
83
if(p->mutex == NULL) {
84
size_t instrNum = static_cast<size_t>(p->h.insdshead->p1);
85
size_t lockNum = static_cast<size_t>(*p->ilocknum);
87
p->mutex = getMutex(csound, instrNum, lockNum);
90
csound->UnlockMutex(p->mutex);
95
#define S(x) sizeof(x)
97
static OENTRY localops[] = {
98
{ (char*)"mutex_lock", S(OPCODE_MUTEX), 2, (char*)"", (char*)"i",
99
NULL, (SUBR)mutexLock, NULL },
100
{ (char*)"mutex_unlock", S(OPCODE_MUTEX), 2, (char*)"", (char*)"i",
101
NULL, (SUBR)mutexUnlock, NULL },
102
{ (char*)"mutex_locki", S(OPCODE_MUTEX), 1, (char*)"", (char*)"i",
103
(SUBR)mutexLock, NULL, NULL },
104
{ (char*)"mutex_unlocki", S(OPCODE_MUTEX), 1, (char*)"", (char*)"i",
105
(SUBR)mutexUnlock, NULL, NULL }