aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/luaproc/channel.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/luaproc/channel.c')
-rw-r--r--libraries/luaproc/channel.c151
1 files changed, 0 insertions, 151 deletions
diff --git a/libraries/luaproc/channel.c b/libraries/luaproc/channel.c
deleted file mode 100644
index ef4ab4b..0000000
--- a/libraries/luaproc/channel.c
+++ /dev/null
@@ -1,151 +0,0 @@
1/***************************************************
2
3Copyright 2008 Alexandre Skyrme, Noemi Rodriguez, Roberto Ierusalimschy
4
5Permission is hereby granted, free of charge, to any person obtaining a copy
6of this software and associated documentation files (the "Software"), to deal
7in the Software without restriction, including without limitation the rights
8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9copies of the Software, and to permit persons to whom the Software is
10furnished to do so, subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21THE SOFTWARE.
22
23*****************************************************
24
25[channel.c]
26
27****************************************************/
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <lua.h>
33#include <lauxlib.h>
34#include <lualib.h>
35
36#include "channel.h"
37#include "list.h"
38
39/* global channel lua_State mutex */
40pthread_mutex_t mutex_channel_lstate = PTHREAD_MUTEX_INITIALIZER;
41
42/* global lua_State where channel hash table will be stored */
43lua_State *chanls = NULL;
44
45/* message channel */
46struct stchannel {
47 list send;
48 list recv;
49 pthread_mutex_t *mutex;
50 pthread_cond_t *in_use;
51};
52
53/* initialize channel table */
54void channel_init( void ) {
55 chanls = luaL_newstate();
56 lua_newtable( chanls );
57 lua_setglobal( chanls, "channeltb" );
58}
59
60/* create new channel */
61channel channel_create( const char *cname ) {
62
63 channel chan;
64
65 /* get exclusive access to the channel table */
66 pthread_mutex_lock( &mutex_channel_lstate );
67
68 /* create a new channel */
69 lua_getglobal( chanls, "channeltb");
70 lua_pushstring( chanls, cname );
71 chan = (channel )lua_newuserdata( chanls, sizeof( struct stchannel ));
72 chan->send = list_new();
73 chan->recv = list_new();
74 chan->mutex = (pthread_mutex_t *)malloc( sizeof( pthread_mutex_t ));
75 pthread_mutex_init( chan->mutex, NULL );
76 chan->in_use = (pthread_cond_t *)malloc( sizeof( pthread_cond_t ));
77 pthread_cond_init( chan->in_use, NULL );
78 lua_settable( chanls, -3 );
79 lua_pop( chanls, 1 );
80
81 /* let others access the channel table */
82 pthread_mutex_unlock( &mutex_channel_lstate );
83
84 return chan;
85}
86
87/* destroy a channel */
88int channel_destroy( channel chan, const char *chname ) {
89
90 /* get exclusive access to the channel table */
91 pthread_mutex_lock( &mutex_channel_lstate );
92
93 list_destroy( chan->send );
94 list_destroy( chan->recv );
95
96 lua_getglobal( chanls, "channeltb");
97 lua_pushstring( chanls, chname );
98 lua_pushnil( chanls );
99 lua_settable( chanls, -3 );
100 lua_pop( chanls, 1 );
101
102 /* let others access the channel table */
103 pthread_mutex_unlock( &mutex_channel_lstate );
104
105 return CHANNEL_DESTROYED;
106}
107
108/* search for and return a channel with a given name */
109channel channel_search( const char *cname ) {
110
111 channel chan;
112
113 /* get exclusive access to the channel table */
114 pthread_mutex_lock( &mutex_channel_lstate );
115
116 /* search for channel */
117 lua_getglobal( chanls, "channeltb");
118 lua_getfield( chanls, -1, cname );
119 if (( lua_type( chanls, -1 )) == LUA_TUSERDATA ) {
120 chan = (channel )lua_touserdata( chanls, -1 );
121 } else {
122 chan = NULL;
123 }
124 lua_pop( chanls, 2 );
125
126 /* let others access channel table */
127 pthread_mutex_unlock( &mutex_channel_lstate );
128
129 return chan;
130}
131
132/* return a channel's send queue */
133list channel_get_sendq( channel chan ) {
134 return chan->send;
135}
136
137/* return a channel's receive queue */
138list channel_get_recvq( channel chan ) {
139 return chan->recv;
140}
141
142/* return a channel's mutex */
143pthread_mutex_t *channel_get_mutex( channel chan ) {
144 return chan->mutex;
145}
146
147/* return a channel's conditional variable */
148pthread_cond_t *channel_get_cond( channel chan ) {
149 return chan->in_use;
150}
151