mutex.c

来自「MANTIS是由科罗拉多大学开发的传感器网络嵌入式操作系统。 这是mantis」· C语言 代码 · 共 93 行

C
93
字号
//  This file is part of MANTIS OS, Operating System//  See http://mantis.cs.colorado.edu/////  Copyright (C) 2003,2004,2005 University of Colorado, Boulder////  This program is free software; you can redistribute it and/or//  modify it under the terms of the mos license (see file LICENSE)/*  Project Mantis  File: mutex.c  Author: Jeff Rose  Date: 1-20-03  Basic mutex support.*/#include "mos.h"#include "mutex.h"void mos_mutex_init(mos_mutex_t *mt){   mt->owner = NULL;   mos_tlist_init(&mt->q);}void mos_mutex_lock(mos_mutex_t *mt){   thread_t *id;   handle_t int_handle;   id = mos_thread_current(); // Get the current thread ID   int_handle = mos_disable_ints();      // If its locked then add the thread to the queue and block   if(mt->owner) {      mos_tlist_add(&mt->q, id);      mos_thread_suspend_noints(int_handle);   } else { // If its free then lock it and save the id      mt->owner = id;      mos_enable_ints(int_handle);   }}void mos_mutex_unlock(mos_mutex_t *mt){   thread_t *next;   thread_t *id;   handle_t int_handle;      id = mos_thread_current(); // Get the current thread ID   int_handle = mos_disable_ints();      // Make sure the current thread is the owner before unlocking   if(mt->owner != id) {     mos_enable_ints(int_handle);     return;   }   // Now if we have a waiting thread make them ready   if((next = mos_tlist_remove(&mt->q))) {      mt->owner = next;      //mos_thread_resume_noints_nodispatch(next, int_handle);      //mos_enable_ints(int_handle);      mos_thread_resume_noints(next, int_handle);   } else { // Just unlock the mutex and clear the owner      mt->owner = NULL;      mos_enable_ints(int_handle);   }}int8_t mos_mutex_try_lock(mos_mutex_t *mt){   thread_t *id;   handle_t int_handle;   id = mos_thread_current(); // Get the current thread ID   int_handle = mos_disable_ints();      // If its locked then return a zero   if(mt->owner) {      mos_enable_ints(int_handle);      return MUTEX_LOCKED;   } else { // If its free then lock it and save the id      mt->owner = id;   }   mos_enable_ints(int_handle);   return MUTEX_OK;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?