-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7aee9db
Showing
13 changed files
with
2,122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
luaproc License | ||
--------------- | ||
|
||
luaproc is licensed under the terms of the MIT license reproduced below. | ||
This means that luaproc is free software and can be used for both academic | ||
and commercial purposes at absolutely no cost. | ||
|
||
=============================================================================== | ||
|
||
Copyright (C) 2008 Alexandre Skyrme, Noemi Rodriguez, Roberto Ierusalimschy | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
|
||
=============================================================================== | ||
|
||
(end of COPYRIGHT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#################################################### | ||
# | ||
# Copyright 2008 Alexandre Skyrme, Noemi Rodriguez, Roberto Ierusalimschy | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in | ||
# all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
# THE SOFTWARE. | ||
# | ||
###################################################### | ||
# | ||
# [Makefile] | ||
# | ||
###################################################### | ||
|
||
# path to lua header files | ||
LUA_INC_PATH=/usr/include/lua5.1 | ||
# path to lua library | ||
LUA_LIB_PATH=/usr/lib/lua5.1 | ||
|
||
# standard makefile variables | ||
CC=gcc | ||
CFLAGS=-c -Wall -fPIC -I${LUA_INC_PATH} | ||
LDFLAGS=-shared -L${LUA_LIB_PATH} -lpthread | ||
SOURCES=sched.c list.c luaproc.c channel.c | ||
OBJECTS=${SOURCES:.c=.o} | ||
LIB=luaproc.so | ||
|
||
all: ${SOURCES} ${LIB} | ||
|
||
${LIB}: ${OBJECTS} | ||
${CC} ${OBJECTS} -o $@ ${LDFLAGS} | ||
|
||
sched.o: sched.c sched.h list.h luaproc.h channel.h | ||
${CC} ${CFLAGS} sched.c | ||
|
||
list.o: list.c list.h | ||
${CC} ${CFLAGS} list.c | ||
|
||
luaproc.o: luaproc.c luaproc.h list.h sched.h channel.h | ||
${CC} ${CFLAGS} luaproc.c | ||
|
||
channel.o: channel.c channel.h list.h | ||
${CC} ${CFLAGS} channel.c | ||
|
||
clean: | ||
rm -f ${OBJECTS} ${LIB} | ||
|
||
test: | ||
lua test.lua | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
*************************************************** | ||
* | ||
* Copyright 2008 Alexandre Skyrme, Noemi Rodriguez, Roberto Ierusalimschy | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
* | ||
***************************************************** | ||
* | ||
* [README] | ||
* | ||
**************************************************** | ||
|
||
|
||
************** | ||
* PARENT API * | ||
************** | ||
|
||
-- Create a new lua process | ||
-- Returns true if sucessful or nil, error_message if failed | ||
luaproc.newproc( <string lua_code> ) | ||
|
||
-- Create a new worker (pthread) | ||
-- Returns true if sucessful or nil, error_message if failed | ||
luaproc.createworker( <void> ) | ||
|
||
-- Destroy a worker (pthread) | ||
-- Returns true if sucessful or nil, error_message if failed | ||
luaproc.destroyworker( <void> ) | ||
|
||
-- Synchronize workers (pthreads) and exit after all lua processes have ended | ||
-- No return, finishes execution. | ||
luaproc.exit( <void> ) | ||
|
||
-- Set maximum lua processes that should be recycled (default = 0) | ||
-- Returns true if sucessful or nil, error_message if failed | ||
luaproc.recycle( <int maxrecycle> ) | ||
|
||
************************************************************ | ||
* CHILD API * | ||
* Available only to processes spawned * | ||
* with luaproc.newproc * | ||
************************************************************ | ||
|
||
-- Create a new lua process | ||
-- Returns true if sucessful or nil, error_message if failed | ||
luaproc.newproc( <string lua_code> ) | ||
|
||
-- Create a new worker (pthread) | ||
-- Returns true if sucessful or nil, error_message if failed | ||
luaproc.createworker( <void> ) | ||
|
||
-- Destroy a worker (pthread) | ||
-- Returns true if sucessful or nil, error_message if failed | ||
luaproc.destroyworker( <void> ) | ||
|
||
-- Send a message on a channel | ||
-- Returns true if sucessful or nil, error_message if failed | ||
-- Results in blocking if there is no matching receive | ||
luaproc.send( <string channel_name>, <string msg1>, | ||
[string msg2], [string msg3], ... ) | ||
|
||
-- Receive a message on a channel | ||
-- Returns message string(s) if sucessful or nil, error_message if failed | ||
-- Results in blocking if there is no matching send | ||
-- and the asynchronous flag is not set (nil) or set to false | ||
luaproc.receive( <string channel_name>, [boolean asynchronous] ) | ||
|
||
-- Create a new channel | ||
-- Returns true if sucessful or nil, error_message if failed | ||
luaproc.newchannel( <string channel_name> ) | ||
|
||
-- Destroy a channel | ||
-- Returns true if sucessful or nil, error_message if failed | ||
luaproc.delchannel( <string channel_name> ) | ||
|
||
-- Set maximum lua processes that should be recycled (default = 0) | ||
-- Returns true if sucessful or nil, error_message if failed | ||
luaproc.recycle( <int maxrecycle> ) | ||
|
||
<> = mandatory arguments | ||
[] = optional arguments |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/*************************************************** | ||
Copyright 2008 Alexandre Skyrme, Noemi Rodriguez, Roberto Ierusalimschy | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
***************************************************** | ||
[channel.c] | ||
****************************************************/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <lua.h> | ||
#include <lauxlib.h> | ||
#include <lualib.h> | ||
|
||
#include "channel.h" | ||
#include "list.h" | ||
|
||
/* global channel lua_State mutex */ | ||
pthread_mutex_t mutex_channel_lstate = PTHREAD_MUTEX_INITIALIZER; | ||
|
||
/* global lua_State where channel hash table will be stored */ | ||
lua_State *chanls = NULL; | ||
|
||
/* message channel */ | ||
struct stchannel { | ||
list send; | ||
list recv; | ||
pthread_mutex_t *mutex; | ||
pthread_cond_t *in_use; | ||
}; | ||
|
||
/* initialize channel table */ | ||
void channel_init( void ) { | ||
chanls = luaL_newstate(); | ||
lua_newtable( chanls ); | ||
lua_setglobal( chanls, "channeltb" ); | ||
} | ||
|
||
/* create new channel */ | ||
channel channel_create( const char *cname ) { | ||
|
||
channel chan; | ||
|
||
/* get exclusive access to the channel table */ | ||
pthread_mutex_lock( &mutex_channel_lstate ); | ||
|
||
/* create a new channel */ | ||
lua_getglobal( chanls, "channeltb"); | ||
lua_pushstring( chanls, cname ); | ||
chan = (channel )lua_newuserdata( chanls, sizeof( struct stchannel )); | ||
chan->send = list_new(); | ||
chan->recv = list_new(); | ||
chan->mutex = (pthread_mutex_t *)malloc( sizeof( pthread_mutex_t )); | ||
pthread_mutex_init( chan->mutex, NULL ); | ||
chan->in_use = (pthread_cond_t *)malloc( sizeof( pthread_cond_t )); | ||
pthread_cond_init( chan->in_use, NULL ); | ||
lua_settable( chanls, -3 ); | ||
lua_pop( chanls, 1 ); | ||
|
||
/* let others access the channel table */ | ||
pthread_mutex_unlock( &mutex_channel_lstate ); | ||
|
||
return chan; | ||
} | ||
|
||
/* destroy a channel */ | ||
int channel_destroy( channel chan, const char *chname ) { | ||
|
||
/* get exclusive access to the channel table */ | ||
pthread_mutex_lock( &mutex_channel_lstate ); | ||
|
||
list_destroy( chan->send ); | ||
list_destroy( chan->recv ); | ||
|
||
lua_getglobal( chanls, "channeltb"); | ||
lua_pushstring( chanls, chname ); | ||
lua_pushnil( chanls ); | ||
lua_settable( chanls, -3 ); | ||
lua_pop( chanls, 1 ); | ||
|
||
/* let others access the channel table */ | ||
pthread_mutex_unlock( &mutex_channel_lstate ); | ||
|
||
return CHANNEL_DESTROYED; | ||
} | ||
|
||
/* search for and return a channel with a given name */ | ||
channel channel_search( const char *cname ) { | ||
|
||
channel chan; | ||
|
||
/* get exclusive access to the channel table */ | ||
pthread_mutex_lock( &mutex_channel_lstate ); | ||
|
||
/* search for channel */ | ||
lua_getglobal( chanls, "channeltb"); | ||
lua_getfield( chanls, -1, cname ); | ||
if (( lua_type( chanls, -1 )) == LUA_TUSERDATA ) { | ||
chan = (channel )lua_touserdata( chanls, -1 ); | ||
} else { | ||
chan = NULL; | ||
} | ||
lua_pop( chanls, 2 ); | ||
|
||
/* let others access channel table */ | ||
pthread_mutex_unlock( &mutex_channel_lstate ); | ||
|
||
return chan; | ||
} | ||
|
||
/* return a channel's send queue */ | ||
list channel_get_sendq( channel chan ) { | ||
return chan->send; | ||
} | ||
|
||
/* return a channel's receive queue */ | ||
list channel_get_recvq( channel chan ) { | ||
return chan->recv; | ||
} | ||
|
||
/* return a channel's mutex */ | ||
pthread_mutex_t *channel_get_mutex( channel chan ) { | ||
return chan->mutex; | ||
} | ||
|
||
/* return a channel's conditional variable */ | ||
pthread_cond_t *channel_get_cond( channel chan ) { | ||
return chan->in_use; | ||
} | ||
|
Oops, something went wrong.