N-sim
Emulation and simulation of
Wireless Sensor Networks



   Home


   Project Page


   Download


   CVS



   Installation


   Configuration


   Plug-ins




 Hosted by
SourceForge.net Logo

/home/brennan/n-sim/OrbisQuartus/shared/mutex-pthread.c

Go to the documentation of this file.
00001 
00014 /*
00015  * Copyright 2007. Los Alamos National Security, LLC. This material
00016  * was produced under U.S. Government contract DE-AC52-06NA25396 for
00017  * Los Alamos National Laboratory (LANL), which is operated by Los
00018  * Alamos National Security, LLC, for the Department of Energy. The
00019  * U.S. Government has rights to use, reproduce, and distribute this
00020  * software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY,
00021  * LLC, MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LEGAL
00022  * LIABILITY FOR THE USE OF THIS SOFTWARE. If software is modified to
00023  * produce derivative works, such modified software should be clearly
00024  * marked, so as not to confuse it with the version available from LANL.
00025  *
00026  * Additionally, this program is free software; you can redistribute
00027  * it and/or modify it under the terms of the GNU General Public
00028  * License as published by the Free Software Foundation; version 2 of
00029  * the License. Accordingly, this program is distributed in the hope
00030  * it will be useful, but WITHOUT ANY WARRANTY; without even the
00031  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00032  * PURPOSE. See the GNU General Public License for more details.
00033  */
00034 
00035 
00036 #include <stdlib.h>
00037 #include <pthread.h>
00038 #include <errno.h>
00039 #include "mutex.h"
00040 
00041 
00042 int mutex_init(mutex_t *m)
00043 {
00044         if (m == 0)
00045                 return -1;
00046         if (m->pthread_mutex == 0) {
00047                 if ((m->pthread_mutex = malloc(sizeof(pthread_mutex_t))) 
00048                     == NULL)
00049                         return -ENOMEM;
00050         }
00051         return pthread_mutex_init((pthread_mutex_t*)m->pthread_mutex, NULL);
00052 }
00053 
00054 
00055 int mutex_lock(mutex_t *m)
00056 {
00057         if (m == 0)
00058                 return -1;
00059         return pthread_mutex_lock((pthread_mutex_t*)m->pthread_mutex);
00060 }
00061 
00062 
00063 int mutex_trylock(mutex_t *m)
00064 {
00065         if (m == 0)
00066                 return -1;
00067         return pthread_mutex_trylock((pthread_mutex_t*)m->pthread_mutex);
00068 }
00069 
00070 
00071 int mutex_unlock(mutex_t *m)
00072 {
00073         if (m == 0)
00074                 return -1;
00075         return pthread_mutex_unlock((pthread_mutex_t*)m->pthread_mutex);
00076 }
00077 
00078 
00079 int mutex_destroy(mutex_t *m)
00080 {
00081         int error = -1;
00082         if (m == 0)
00083                 return error;
00084 
00085         if (m->pthread_mutex != 0) {
00086                 error = pthread_mutex_destroy(
00087                         (pthread_mutex_t*)m->pthread_mutex);
00088                 free(m->pthread_mutex);
00089                 m->pthread_mutex = 0;
00090         }
00091         return error;
00092 }


© 2007, Los Alamos National Security, LLC.