Skip to content

Commit

Permalink
Added critical sections for thread protection.
Browse files Browse the repository at this point in the history
  • Loading branch information
malloch committed Feb 1, 2020
1 parent d30de0d commit f6a91e3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ This software is licensed with the GPLv3; see the attached file
COPYING for details, which should be included in this download.

----------
Joseph Malloch 2013–2017
Joseph Malloch 2013–2020
Graphics and Experiential Media Lab, Dalhousie University
Input Devices and Music Interaction Laboratory, McGill University.

http://libmapper.org
Expand Down
28 changes: 24 additions & 4 deletions mapdevice/map.device.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ typedef struct _mapdevice
int ready;
t_atom buffer[MAX_LIST];
t_object *patcher;
int throttle;
} t_mapdevice;

typedef struct _map_ptrs
Expand Down Expand Up @@ -125,6 +126,7 @@ static void *mapdevice_new(t_symbol *s, int argc, t_atom *argv)
if ((x = object_alloc(mapdevice_class))) {
x->outlet = listout((t_object *)x);
x->name = 0;
x->throttle = 10;

if (argv->a_type == A_SYM && atom_get_string(argv)[0] != '@')
alias = atom_get_string(argv);
Expand All @@ -143,6 +145,20 @@ static void *mapdevice_new(t_symbol *s, int argc, t_atom *argv)
i++;
}
}
else if (atom_strcmp(argv+i, "@throttle") == 0) {
if ((argv+i+1)->a_type == A_LONG) {
int throttle = atom_getlong(argv+i+1);
if (throttle > 0)
x->throttle = throttle;
i++;
}
else if ((argv+i+1)->a_type == A_FLOAT) {
int throttle = (int)atom_getfloat(argv+i+1);
if (throttle > 0)
x->throttle = throttle;
i++;
}
}
}
}
if (alias) {
Expand Down Expand Up @@ -226,7 +242,7 @@ static void *mapdevice_new(t_symbol *s, int argc, t_atom *argv)

// Create the timing clock
x->clock = clock_new(x, (method)mapdevice_poll);
clock_fdelay(x->clock, INTERVAL); // Set clock to go off after delay
clock_delay(x->clock, INTERVAL); // Set clock to go off after delay
}
return (x);
}
Expand Down Expand Up @@ -499,6 +515,10 @@ static void mapdevice_print_properties(t_mapdevice *x)
atom_setlong(x->buffer, mapper_device_num_signals(x->device,
MAPPER_DIR_OUTGOING));
outlet_anything(x->outlet, gensym("numOutputs"), 1, x->buffer);

//output throttle
atom_setlong(x->buffer, x->throttle);
outlet_anything(x->outlet, gensym("throttle"), 1, x->buffer);
}
}

Expand Down Expand Up @@ -630,8 +650,8 @@ static void mapdevice_maybe_start_queue(t_mapdevice *x)
// -(poll libmapper)----------------------------------------
static void mapdevice_poll(t_mapdevice *x)
{
int count = 10;
while(count-- && mapper_device_poll(x->device, 0)) {};
int count = x->throttle;
while (count-- && mapper_device_poll(x->device, 0)) {};
if (!x->ready) {
if (mapper_device_ready(x->device)) {
object_post((t_object *)x, "Joining mapping network as '%s'",
Expand All @@ -646,7 +666,7 @@ static void mapdevice_poll(t_mapdevice *x)
mapper_device_send_queue(x->device, x->timetag);
x->updated = 0;
}
clock_fdelay(x->clock, INTERVAL); // Set clock to go off after delay
clock_delay(x->clock, INTERVAL); // Set clock to go off after delay
}


Expand Down
10 changes: 10 additions & 0 deletions mapin/map.in.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,12 +485,14 @@ static void mapin_int(t_mapin *x, long l)
f = (float)l;
value = &f;
}
critical_enter(0);
object_method(x->dev_obj, maybe_start_queue_sym);
if (x->is_instance)
mapper_signal_instance_update(x->sig_ptr, x->instance_id,
value, 1, *x->tt_ptr);
else
mapper_signal_update(x->sig_ptr, value, 1, *x->tt_ptr);
critical_exit(0);
}

// *********************************************************
Expand All @@ -514,12 +516,14 @@ static void mapin_float(t_mapin *x, double d)
i = (int)d;
value = &i;
}
critical_enter(0);
object_method(x->dev_obj, maybe_start_queue_sym);
if (x->is_instance)
mapper_signal_instance_update(x->sig_ptr, x->instance_id,
value, 1, *x->tt_ptr);
else
mapper_signal_update(x->sig_ptr, value, 1, *x->tt_ptr);
critical_exit(0);
}

// *********************************************************
Expand Down Expand Up @@ -553,6 +557,7 @@ static void mapin_list(t_mapin *x, t_symbol *s, int argc, t_atom *argv)
}
}
//update signal
critical_enter(0);
object_method(x->dev_obj, maybe_start_queue_sym);
if (x->is_instance) {
mapper_signal_instance_update(x->sig_ptr, x->instance_id,
Expand All @@ -561,6 +566,7 @@ static void mapin_list(t_mapin *x, t_symbol *s, int argc, t_atom *argv)
else {
mapper_signal_update(x->sig_ptr, value, count, *x->tt_ptr);
}
critical_exit(0);
}
else if (x->type == 'f') {
float payload[argc];
Expand All @@ -576,6 +582,7 @@ static void mapin_list(t_mapin *x, t_symbol *s, int argc, t_atom *argv)
}
}
//update signal
critical_enter(0);
object_method(x->dev_obj, maybe_start_queue_sym);
if (x->is_instance) {
mapper_signal_instance_update(x->sig_ptr, x->instance_id,
Expand All @@ -584,6 +591,7 @@ static void mapin_list(t_mapin *x, t_symbol *s, int argc, t_atom *argv)
else {
mapper_signal_update(x->sig_ptr, value, count, *x->tt_ptr);
}
critical_exit(0);
}
}

Expand All @@ -608,8 +616,10 @@ static void mapin_release(t_mapin *x)
if (check_ptrs(x) || !x->is_instance)
return;

critical_enter(0);
object_method(x->dev_obj, maybe_start_queue_sym);
mapper_signal_instance_release(x->sig_ptr, x->instance_id, *x->tt_ptr);
critical_exit(0);
}

// *********************************************************
Expand Down
10 changes: 10 additions & 0 deletions mapout/map.out.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,12 +487,14 @@ static void mapout_int(t_mapout *x, long l)
f = (float)l;
value = &f;
}
critical_enter(0);
object_method(x->dev_obj, maybe_start_queue_sym);
if (x->is_instance)
mapper_signal_instance_update(x->sig_ptr, x->instance_id,
value, 1, *x->tt_ptr);
else
mapper_signal_update(x->sig_ptr, value, 1, *x->tt_ptr);
critical_exit(0);
}

// *********************************************************
Expand All @@ -516,12 +518,14 @@ static void mapout_float(t_mapout *x, double d)
i = (int)d;
value = &i;
}
critical_enter(0);
object_method(x->dev_obj, maybe_start_queue_sym);
if (x->is_instance)
mapper_signal_instance_update(x->sig_ptr, x->instance_id,
value, 1, *x->tt_ptr);
else
mapper_signal_update(x->sig_ptr, value, 1, *x->tt_ptr);
critical_exit(0);
}

// *********************************************************
Expand Down Expand Up @@ -555,6 +559,7 @@ static void mapout_list(t_mapout *x, t_symbol *s, int argc, t_atom *argv)
}
}
//update signal
critical_enter(0);
object_method(x->dev_obj, maybe_start_queue_sym);
if (x->is_instance) {
mapper_signal_instance_update(x->sig_ptr, x->instance_id,
Expand All @@ -563,6 +568,7 @@ static void mapout_list(t_mapout *x, t_symbol *s, int argc, t_atom *argv)
else {
mapper_signal_update(x->sig_ptr, value, count, *x->tt_ptr);
}
critical_exit(0);
}
else if (x->type == 'f') {
float payload[argc];
Expand All @@ -577,6 +583,7 @@ static void mapout_list(t_mapout *x, t_symbol *s, int argc, t_atom *argv)
return;
}
}
critical_enter(0);
//update signal
object_method(x->dev_obj, maybe_start_queue_sym);
if (x->is_instance) {
Expand All @@ -586,6 +593,7 @@ static void mapout_list(t_mapout *x, t_symbol *s, int argc, t_atom *argv)
else {
mapper_signal_update(x->sig_ptr, value, count, *x->tt_ptr);
}
critical_exit(0);
}
}

Expand Down Expand Up @@ -623,8 +631,10 @@ static void mapout_release(t_mapout *x)
if (check_ptrs(x) || !x->is_instance)
return;

critical_enter(0);
object_method(x->dev_obj, maybe_start_queue_sym);
mapper_signal_instance_release(x->sig_ptr, x->instance_id, *x->tt_ptr);
critical_exit(0);
}

// *********************************************************
Expand Down

0 comments on commit f6a91e3

Please sign in to comment.