Skip to content

Commit

Permalink
Replace most semaphore manual up-down with semaphore guard
Browse files Browse the repository at this point in the history
  • Loading branch information
rpavlik committed Jun 22, 2015
1 parent ccdd62e commit c72207f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 77 deletions.
27 changes: 6 additions & 21 deletions vrpn_BaseClass.C
Expand Up @@ -25,7 +25,7 @@ vrpn_TextPrinter::vrpn_TextPrinter()
/** Deletes any callbacks that are still registered. */
vrpn_TextPrinter::~vrpn_TextPrinter()
{
d_semaphore.p();
vrpn::SemaphoreGuard guard(d_semaphore);
/* XXX No longer removes these. We get into trouble with the
system-defined vrpn_System_TextPrinter destructor because it
may run after the vrpn_ConnectionManager destructor has run,
Expand All @@ -52,7 +52,6 @@ vrpn_TextPrinter::~vrpn_TextPrinter()
victim = next;
}
#endif // XXX
d_semaphore.v();
}

/** Adds an object to the list of watched objects. Returns 0 on success and
Expand All @@ -66,14 +65,13 @@ vrpn_TextPrinter::~vrpn_TextPrinter()
*/
int vrpn_TextPrinter::add_object(vrpn_BaseClass *o)
{
d_semaphore.p();
vrpn::SemaphoreGuard guard(d_semaphore);
vrpn_TextPrinter_Watch_Entry *victim;

// Make sure we have an actual object.
if (o == NULL) {
fprintf(stderr,
"vrpn_TextPrinter::add_object(): NULL pointer passed\n");
d_semaphore.v();
return -1;
}

Expand All @@ -88,7 +86,6 @@ int vrpn_TextPrinter::add_object(vrpn_BaseClass *o)
while (victim != NULL) {
if ((o->d_connection == victim->obj->d_connection) &&
(strcmp(o->d_servicename, victim->obj->d_servicename) == 0)) {
d_semaphore.v();
return 0;
}
victim = victim->next;
Expand All @@ -97,7 +94,6 @@ int vrpn_TextPrinter::add_object(vrpn_BaseClass *o)
// Add the object to the beginning of the list.
if ((victim = new vrpn_TextPrinter_Watch_Entry) == NULL) {
fprintf(stderr, "vrpn_TextPrinter::add_object(): out of memory\n");
d_semaphore.v();
return -1;
}
victim->obj = o;
Expand All @@ -113,11 +109,9 @@ int vrpn_TextPrinter::add_object(vrpn_BaseClass *o)
"vrpn_TextPrinter::add_object(): Can't register callback\n");
d_first_watched_object = victim->next;
delete victim;
d_semaphore.v();
return -1;
}

d_semaphore.v();
return 0;
}

Expand All @@ -128,7 +122,7 @@ int vrpn_TextPrinter::add_object(vrpn_BaseClass *o)
*/
void vrpn_TextPrinter::remove_object(vrpn_BaseClass *o)
{
d_semaphore.p();
vrpn::SemaphoreGuard guard(d_semaphore);
vrpn_TextPrinter_Watch_Entry *victim, **snitch;

#ifdef VERBOSE
Expand All @@ -139,7 +133,6 @@ void vrpn_TextPrinter::remove_object(vrpn_BaseClass *o)
if (o == NULL) {
fprintf(stderr,
"vrpn_TextPrinter::remove_object(): NULL pointer passed\n");
d_semaphore.v();
return;
}

Expand Down Expand Up @@ -177,12 +170,10 @@ void vrpn_TextPrinter::remove_object(vrpn_BaseClass *o)
delete victim;

// We're done.
d_semaphore.v();
return;
}

// Object not in the list, so we're done.
d_semaphore.v();
return;
}

Expand All @@ -202,8 +193,7 @@ int vrpn_TextPrinter::text_message_handler(void *userdata, vrpn_HANDLERPARAM p)
vrpn_TEXT_SEVERITY severity;
vrpn_uint32 level;
char message[vrpn_MAX_TEXT_LEN];

me->d_semaphore.p();
vrpn::SemaphoreGuard guard(me->d_semaphore);

#ifdef VERBOSE
printf("vrpn_TextPrinter: text handler called\n");
Expand All @@ -220,7 +210,6 @@ int vrpn_TextPrinter::text_message_handler(void *userdata, vrpn_HANDLERPARAM p)
fprintf(
stderr,
"vrpn_TextPrinter::text_message_handler(): Can't decode message\n");
me->d_semaphore.v();
return -1;
}

Expand Down Expand Up @@ -254,8 +243,6 @@ int vrpn_TextPrinter::text_message_handler(void *userdata, vrpn_HANDLERPARAM p)
fprintf(me->d_ostream, " (%d) from %s: %s\n", level,
obj->d_connection->sender_name(p.sender), message);
}

me->d_semaphore.v();
return 0;
}

Expand All @@ -264,19 +251,17 @@ int vrpn_TextPrinter::text_message_handler(void *userdata, vrpn_HANDLERPARAM p)
void vrpn_TextPrinter::set_min_level_to_print(vrpn_TEXT_SEVERITY severity,
vrpn_uint32 level)
{
d_semaphore.p();
vrpn::SemaphoreGuard guard(d_semaphore);
d_severity_to_print = severity;
d_level_to_print = level;
d_semaphore.v();
}

/// Change the ostream that will be used to print messages. Setting a
/// NULL ostream results in no printing.
void vrpn_TextPrinter::set_ostream_to_use(FILE *o)
{
d_semaphore.p();
vrpn::SemaphoreGuard guard(d_semaphore);
d_ostream = o;
d_semaphore.v();
}

/** Assigns the connection passed in to the object, or else tries to
Expand Down
44 changes: 22 additions & 22 deletions vrpn_Connection.C
Expand Up @@ -1426,7 +1426,7 @@ void vrpn_TypeDispatcher::clear(void)

vrpn_ConnectionManager::~vrpn_ConnectionManager(void)
{
d_semaphore.p();
vrpn::SemaphoreGuard guard(d_semaphore);
// fprintf(stderr, "In ~vrpn_ConnectionManager: tearing down the list.\n");

// Call the destructor of every known connection.
Expand All @@ -1438,7 +1438,6 @@ vrpn_ConnectionManager::~vrpn_ConnectionManager(void)
while (d_anonList) {
delete d_anonList->connection;
}
d_semaphore.v();
}

// static
Expand All @@ -1449,39 +1448,40 @@ vrpn_ConnectionManager &vrpn_ConnectionManager::instance(void)
// This avoids a race on the constructor of the static
// instance.
static vrpn_Semaphore sem;
sem.p();
vrpn::SemaphoreGuard guard(sem);
static vrpn_ConnectionManager manager;
sem.v();
return manager;
}

void vrpn_ConnectionManager::addConnection(vrpn_Connection *c, const char *name)
{
d_semaphore.p();
knownConnection *p;
vrpn::SemaphoreGuard guard(d_semaphore);
{
knownConnection *p;

p = new knownConnection;
p->connection = c;
p = new knownConnection;
p->connection = c;

if (name) {
strncpy(p->name, name, 1000);
p->next = d_kcList;
d_kcList = p;
}
else {
p->name[0] = 0;
p->next = d_anonList;
d_anonList = p;
if (name) {
strncpy(p->name, name, 1000);
p->next = d_kcList;
d_kcList = p;
}
else {
p->name[0] = 0;
p->next = d_anonList;
d_anonList = p;
}
}
d_semaphore.v();
}

void vrpn_ConnectionManager::deleteConnection(vrpn_Connection *c)
{
d_semaphore.p();
deleteConnection(c, &d_kcList);
deleteConnection(c, &d_anonList);
d_semaphore.v();
vrpn::SemaphoreGuard guard(d_semaphore);
{
deleteConnection(c, &d_kcList);
deleteConnection(c, &d_anonList);
}
}

void vrpn_ConnectionManager::deleteConnection(vrpn_Connection *c,
Expand Down

0 comments on commit c72207f

Please sign in to comment.