summaryrefslogtreecommitdiff
path: root/components/pthreads/pthread_mutex.c
diff options
context:
space:
mode:
authoryiyue.fang <[email protected]>2013-06-26 23:18:30 +0800
committeryiyue.fang <[email protected]>2013-06-26 23:18:30 +0800
commit8ab2b287b813edc1cdbe8bba430a2593e5fc3e8a (patch)
tree1f8534cb1927cb82e38563a26739cc653104c59f /components/pthreads/pthread_mutex.c
parent773990abdb90078839ea0834fc15539e94a04fdf (diff)
Embedded GPLv2 license in pthreads
Diffstat (limited to 'components/pthreads/pthread_mutex.c')
-rw-r--r--components/pthreads/pthread_mutex.c239
1 files changed, 142 insertions, 97 deletions
diff --git a/components/pthreads/pthread_mutex.c b/components/pthreads/pthread_mutex.c
index 34ff91e451..40fd870e81 100644
--- a/components/pthreads/pthread_mutex.c
+++ b/components/pthreads/pthread_mutex.c
@@ -1,3 +1,27 @@
+/*
+ * File : pthread_mutex.c
+ * This file is part of RT-Thread RTOS
+ * COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2010-10-26 Bernard the first version
+ */
+
#include <rtthread.h>
#include "pthread.h"
@@ -8,51 +32,54 @@ const pthread_mutexattr_t pthread_default_mutexattr = PTHREAD_PROCESS_PRIVATE;
int pthread_mutexattr_init(pthread_mutexattr_t *attr)
{
- if (attr)
- {
- *attr = pthread_default_mutexattr;
- return 0;
- }
+ if (attr)
+ {
+ *attr = pthread_default_mutexattr;
+
+ return 0;
+ }
- return EINVAL;
+ return EINVAL;
}
RTM_EXPORT(pthread_mutexattr_init);
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
{
- if (attr)
- {
- *attr = -1;
- return 0;
- }
+ if (attr)
+ {
+ *attr = -1;
- return EINVAL;
+ return 0;
+ }
+
+ return EINVAL;
}
RTM_EXPORT(pthread_mutexattr_destroy);
int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type)
{
- if (attr && type)
- {
+ if (attr && type)
+ {
int atype = (*attr & MUTEXATTR_TYPE_MASK);
- if (atype >= PTHREAD_MUTEX_NORMAL && atype <= PTHREAD_MUTEX_ERRORCHECK)
- {
- *type = atype;
- return 0;
- }
- }
+ if (atype >= PTHREAD_MUTEX_NORMAL && atype <= PTHREAD_MUTEX_ERRORCHECK)
+ {
+ *type = atype;
+
+ return 0;
+ }
+ }
- return EINVAL;
+ return EINVAL;
}
RTM_EXPORT(pthread_mutexattr_gettype);
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
{
- if (attr && type >= PTHREAD_MUTEX_NORMAL &&
- type <= PTHREAD_MUTEX_ERRORCHECK )
- {
+ if (attr && type >= PTHREAD_MUTEX_NORMAL && type <= PTHREAD_MUTEX_ERRORCHECK)
+ {
*attr = (*attr & ~MUTEXATTR_TYPE_MASK) | type;
+
return 0;
}
@@ -60,12 +87,13 @@ int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
}
RTM_EXPORT(pthread_mutexattr_settype);
-int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
+int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
{
- if (!attr) return EINVAL;
+ if (!attr)
+ return EINVAL;
switch (pshared)
- {
+ {
case PTHREAD_PROCESS_PRIVATE:
*attr &= ~MUTEXATTR_SHARED_MASK;
return 0;
@@ -81,7 +109,8 @@ RTM_EXPORT(pthread_mutexattr_setpshared);
int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared)
{
- if (!attr || !pshared) return EINVAL;
+ if (!attr || !pshared)
+ return EINVAL;
*pshared = (*attr & MUTEXATTR_SHARED_MASK) ? PTHREAD_PROCESS_SHARED
: PTHREAD_PROCESS_PRIVATE;
@@ -91,116 +120,132 @@ RTM_EXPORT(pthread_mutexattr_getpshared);
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
{
- rt_err_t result;
- char name[RT_NAME_MAX];
- static rt_uint16_t pthread_mutex_number = 0;
+ rt_err_t result;
+ char name[RT_NAME_MAX];
+ static rt_uint16_t pthread_mutex_number = 0;
- if (!mutex) return EINVAL;
+ if (!mutex)
+ return EINVAL;
- /* build mutex name */
- rt_snprintf(name, sizeof(name), "pmtx%02d", pthread_mutex_number ++);
- if (attr == RT_NULL) mutex->attr = pthread_default_mutexattr;
- else mutex->attr = *attr;
+ /* build mutex name */
+ rt_snprintf(name, sizeof(name), "pmtx%02d", pthread_mutex_number ++);
+ if (attr == RT_NULL)
+ mutex->attr = pthread_default_mutexattr;
+ else
+ mutex->attr = *attr;
- /* init mutex lock */
- result = rt_mutex_init(&(mutex->lock), name, RT_IPC_FLAG_FIFO);
- if (result != RT_EOK) return EINVAL;
+ /* init mutex lock */
+ result = rt_mutex_init(&(mutex->lock), name, RT_IPC_FLAG_FIFO);
+ if (result != RT_EOK)
+ return EINVAL;
- /* detach the object from system object container */
- rt_object_detach(&(mutex->lock.parent.parent));
+ /* detach the object from system object container */
+ rt_object_detach(&(mutex->lock.parent.parent));
- return 0;
+ return 0;
}
RTM_EXPORT(pthread_mutex_init);
int pthread_mutex_destroy(pthread_mutex_t *mutex)
{
- if (!mutex || mutex->attr == -1) return EINVAL;
+ if (!mutex || mutex->attr == -1)
+ return EINVAL;
- /* it's busy */
- if (mutex->lock.owner != RT_NULL) return EBUSY;
+ /* it's busy */
+ if (mutex->lock.owner != RT_NULL)
+ return EBUSY;
- rt_memset(mutex, 0, sizeof(pthread_mutex_t));
- mutex->attr = -1;
+ rt_memset(mutex, 0, sizeof(pthread_mutex_t));
+ mutex->attr = -1;
- return 0;
+ return 0;
}
RTM_EXPORT(pthread_mutex_destroy);
int pthread_mutex_lock(pthread_mutex_t *mutex)
{
- int mtype;
- rt_err_t result;
+ int mtype;
+ rt_err_t result;
- if (!mutex) return EINVAL;
+ if (!mutex)
+ return EINVAL;
- if (mutex->attr == -1)
- {
- /* init mutex */
- pthread_mutex_init(mutex, RT_NULL);
- }
+ if (mutex->attr == -1)
+ {
+ /* init mutex */
+ pthread_mutex_init(mutex, RT_NULL);
+ }
- mtype = mutex->attr & MUTEXATTR_TYPE_MASK;
- rt_enter_critical();
- if (mutex->lock.owner == rt_thread_self() && mtype != PTHREAD_MUTEX_RECURSIVE)
- {
- rt_exit_critical();
- return EDEADLK;
- }
- rt_exit_critical();
+ mtype = mutex->attr & MUTEXATTR_TYPE_MASK;
+ rt_enter_critical();
+ if (mutex->lock.owner == rt_thread_self() &&
+ mtype != PTHREAD_MUTEX_RECURSIVE)
+ {
+ rt_exit_critical();
- result = rt_mutex_take(&(mutex->lock), RT_WAITING_FOREVER);
- if (result == RT_EOK) return 0;
+ return EDEADLK;
+ }
+ rt_exit_critical();
- return EINVAL;
+ result = rt_mutex_take(&(mutex->lock), RT_WAITING_FOREVER);
+ if (result == RT_EOK)
+ return 0;
+
+ return EINVAL;
}
RTM_EXPORT(pthread_mutex_lock);
int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
- rt_err_t result;
+ rt_err_t result;
- if (!mutex) return EINVAL;
- if (mutex->attr == -1)
- {
- /* init mutex */
- pthread_mutex_init(mutex, RT_NULL);
- }
+ if (!mutex)
+ return EINVAL;
+ if (mutex->attr == -1)
+ {
+ /* init mutex */
+ pthread_mutex_init(mutex, RT_NULL);
+ }
- if (mutex->lock.owner != rt_thread_self())
- {
- int mtype;
- mtype = mutex->attr & MUTEXATTR_TYPE_MASK;
+ if (mutex->lock.owner != rt_thread_self())
+ {
+ int mtype;
+ mtype = mutex->attr & MUTEXATTR_TYPE_MASK;
- /* error check, return EPERM */
- if (mtype == PTHREAD_MUTEX_ERRORCHECK) return EPERM;
+ /* error check, return EPERM */
+ if (mtype == PTHREAD_MUTEX_ERRORCHECK)
+ return EPERM;
- /* no thread waiting on this mutex */
- if (mutex->lock.owner == RT_NULL) return 0;
- }
+ /* no thread waiting on this mutex */
+ if (mutex->lock.owner == RT_NULL)
+ return 0;
+ }
- result = rt_mutex_release(&(mutex->lock));
- if (result == RT_EOK) return 0;
-
- return EINVAL;
+ result = rt_mutex_release(&(mutex->lock));
+ if (result == RT_EOK)
+ return 0;
+
+ return EINVAL;
}
RTM_EXPORT(pthread_mutex_unlock);
int pthread_mutex_trylock(pthread_mutex_t *mutex)
{
- rt_err_t result;
+ rt_err_t result;
- if (!mutex) return EINVAL;
- if (mutex->attr == -1)
- {
- /* init mutex */
- pthread_mutex_init(mutex, RT_NULL);
- }
+ if (!mutex)
+ return EINVAL;
+ if (mutex->attr == -1)
+ {
+ /* init mutex */
+ pthread_mutex_init(mutex, RT_NULL);
+ }
- result = rt_mutex_take(&(mutex->lock), 0);
- if (result == RT_EOK) return 0;
+ result = rt_mutex_take(&(mutex->lock), 0);
+ if (result == RT_EOK)
+ return 0;
- return EBUSY;
+ return EBUSY;
}
RTM_EXPORT(pthread_mutex_trylock);