Skip to content

add broadcast operation for condition variable #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,11 @@ has been raised.

Raise the condition signal.

<a name='condition.broadcast'/>
#### Condition.broadcast() ####

Broadcast the condition signal to all waiting threads.

<a name='condition.free'/>
#### Condition.free() ####

Expand Down
7 changes: 7 additions & 0 deletions lib/THThread.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ int THCondition_signal(THCondition *self)
return 0;
}

int THCondition_broadcast(THCondition *self)
{
if(pthread_cond_broadcast(&self->id))
return 1;
return 0;
}

int THCondition_wait(THCondition *self, THMutex *mutex)
{
if(pthread_cond_wait(&self->id, &mutex->id))
Expand Down
1 change: 1 addition & 0 deletions lib/THThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ THCondition* THCondition_new(void);
THCondition* THCondition_newWithId(AddressType id);
AddressType THCondition_id(THCondition *self);
int THCondition_signal(THCondition *self);
int THCondition_broadcast(THCondition *self);
int THCondition_wait(THCondition *self, THMutex *mutex);
void THCondition_free(THCondition *self);

Expand Down
9 changes: 9 additions & 0 deletions lib/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ static int condition_signal(lua_State *L)
return 0;
}

static int condition_broadcast(lua_State *L)
{
THCondition *condition = luaTHRD_checkudata(L, 1, "threads.Condition");
if(THCondition_broadcast(condition))
luaL_error(L, "threads: condition signal failed");
return 0;
}

static int condition_wait(lua_State *L)
{
THCondition *condition = luaTHRD_checkudata(L, 1, "threads.Condition");
Expand Down Expand Up @@ -227,6 +235,7 @@ static const struct luaL_Reg condition__ [] = {
{"__tostring", condition_tostring},
{"id", condition_id},
{"signal", condition_signal},
{"broadcast", condition_broadcast},
{"wait", condition_wait},
{"free", condition_free},
{NULL, NULL}
Expand Down