加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

OS21操作系统——信号量的初始化及创建

(2007-12-15 23:20:13)
标签:

it/科技

分类: 机顶盒设计

信号量(Semaphores),用于任务间同步,以及互斥访问共享资源。信号量结构

体semaphore_t中主要包括三部分:
    1)信号量的计数值
   2)等待信号量的任务队列

3)信号量所占用的内存分区

信号量用如下结构体类型来描述:

struct  semaphore_s

{

       os21_list_t taskq;       

       partition_t * pp;        

       int           count;     

      #if defined CONF_DEBUG_CHECK_SEM

      os21_list_node_t debugNode;   

      #endif

};

 

信号量的创建使用如下函数:

sempahore_t  *semaphre_create_fifo(int value);

sempahore_t  *semaphre_create_fifo_p(paritition_t *parition,int value);

sempahore_t  *semaphre_create_priority(int value);

sempahore_t  *semaphre_create_priority_p(paritition_t *parition,int

value);

注意:由于OS20平台硬件上提供了信号量,使得不超时和超时所调的创建函数不相同,而OS21平台上则不存在这种情况。

OS21提供了两种类型的信号量,任务在等待队列中的排列方式有所不同:

1)FIFO Semaphores:
   顺序获取;任务根据它们调用 semaphore_wait() 函数的先后顺序获取信号量
。这种信号量是由semaphore_create_fifo()或semaphore_create_fifo_p()创建的。

2)Priority Semaphores:
  允许任务在队列间跳跃;最高优先级的任务最先获得信号量,这种信号量是

通过调用semaphore_create_priority()或semaphore_create_priority_p()实现的。

 以seamphore_create_fifo()为例,其代码如下:

semaphore_t * semaphore_create_fifo (int count)

{

  OS21_ASSERT (!_scheduler_system_context ());

  return (semaphore_create_fifo_p (_system_pp, count));

}

semaphore_t * semaphore_create_fifo_p (partition_t * pp, int count)

{

  semaphore_t * sp;

  OS21_ASSERT (!_scheduler_system_context ());

  sp = _semaphore_create_generic (pp);

  if (sp)

  {

    _semaphore_init_fifo (sp, count);

  }

  return (sp);

}

static semaphore_t * _semaphore_create_generic (partition_t * pp)

{

  semaphore_t * sp;

  OS21_ASSERT (!_scheduler_system_context ());

  sp = (semaphore_t *) memory_allocate (pp, sizeof (semaphore_t));

  if (sp)

  {

    sp->pp = pp;

  }

  return (sp);

}

void _semaphore_init_fifo (semaphore_t * sp, int count)

{

  OS21_ASSERT (sp);

  OS21_ASSERT (!_scheduler_system_context ());

  sp->count = count;

  _os21_list_init_fifo (&sp->taskq);

#if defined CONF_DEBUG_CHECK_SEM

  _os21_list_node_init (&(sp->debugNode), (void *) sp, NULL);

#endif

  _CHECK_ADD_SEM_OK(sp);

}

当创建一个信号量时,调用_semaphore_create_generic ()为该信号量分配空间,然后调用_semaphore_init_fifo()初始化信号量结构体的的其他成员。

 

 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有