~ubuntu-branches/ubuntu/precise/csound/precise

« back to all changes in this revision

Viewing changes to Opcodes/mutexops.cpp

  • Committer: Package Import Robot
  • Author(s): Felipe Sateler
  • Date: 2012-04-19 09:26:46 UTC
  • mfrom: (3.2.19 sid)
  • Revision ID: package-import@ubuntu.com-20120419092646-96xbj1n6atuqosk2
Tags: 1:5.17.6~dfsg-1
* New upstream release
 - Do not build the wiimote opcodes (we need wiiuse).
* Add new API function to symbols file
* Disable lua opcodes, they were broken. Requires OpenMP to be enabled.
* Backport fixes from upstream:
  - Link dssi4cs with dl. Backport
  - Fix building of CsoundAC

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    mutexops.cpp:
3
 
 
4
 
    Copyright (C) 2007 by Steven Yi
5
 
 
6
 
    Mutex lock and unlocking opcodes, used for when running Csound with
7
 
    multiple threads
8
 
 
9
 
    This file is part of Csound.
10
 
 
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.
15
 
 
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.
20
 
 
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
24
 
    02111-1307 USA
25
 
*/
26
 
#include "csdl.h"
27
 
#include "csGblMtx.h"
28
 
#include <map>
29
 
 
30
 
static std::map<CSOUND *, std::map<size_t, std::map<size_t, void *> > > mutexes;
31
 
 
32
 
typedef struct {
33
 
        OPDS    h;
34
 
        MYFLT   *ilocknum;
35
 
        void    *mutex;
36
 
} OPCODE_MUTEX;
37
 
 
38
 
static void * getMutex(CSOUND *csound, size_t instrNum, size_t lockNum)
39
 
{
40
 
 
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;
45
 
    }
46
 
 
47
 
    if(mutexes[csound].find(instrNum) == mutexes[csound].end()) {
48
 
        std::map<size_t, void*> mutexMap;
49
 
        mutexes[csound][instrNum] = mutexMap;
50
 
    }
51
 
 
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;
56
 
 
57
 
        csound->Message(csound, "Created new mutex [%ld:%ld]\n",
58
 
                        (long)instrNum, (long)lockNum);
59
 
    }
60
 
    csound_global_mutex_unlock();
61
 
 
62
 
    return mutexes[csound][instrNum][lockNum];
63
 
 
64
 
}
65
 
 
66
 
static int mutexLock(CSOUND *csound, OPCODE_MUTEX *p)
67
 
{
68
 
 
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);
72
 
 
73
 
        p->mutex = getMutex(csound, instrNum, lockNum);
74
 
    }
75
 
 
76
 
    csound->LockMutex(p->mutex);
77
 
 
78
 
    return OK;
79
 
}
80
 
 
81
 
static int mutexUnlock(CSOUND *csound, OPCODE_MUTEX *p)
82
 
{
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);
86
 
 
87
 
        p->mutex = getMutex(csound, instrNum, lockNum);
88
 
    }
89
 
 
90
 
    csound->UnlockMutex(p->mutex);
91
 
 
92
 
    return OK;
93
 
}
94
 
 
95
 
#define S(x)    sizeof(x)
96
 
 
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 }
106
 
};
107
 
 
108
 
 
109
 
LINKAGE