Subversion Repositories HelenOS

Rev

Rev 2938 | Rev 2940 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2008 Jiri Svoboda
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup debug
  30.  * @{
  31.  */
  32. /** @file
  33.  */
  34.  
  35. #include <stdlib.h>
  36. #include <libadt/list.h>
  37. #include <assert.h>
  38. #include <futex.h>
  39. #include <async.h>
  40.  
  41. #include "dthread.h"
  42.  
  43. static int last_thread_id = 0;
  44.  
  45. link_t dthreads;        /* List of application's threads */
  46. dthread_t *cwt;         /* Current working thread */
  47.  
  48. dthread_t *dthread_new(thash_t hash)
  49. {
  50.     dthread_t *dt;
  51.  
  52.     dt = malloc(sizeof(dthread_t));
  53.     if (!dt) {
  54.         exit(1);
  55.     }
  56.  
  57.     dt->id = ++last_thread_id;
  58.     dt->hash = hash;
  59.     dt->fid = 0;
  60.     dt->stopped = 0;
  61.  
  62.     if (!cwt) cwt = dt;
  63.  
  64.     list_append(&dt->link, &dthreads);
  65.  
  66.     return dt;
  67. }
  68.  
  69. void dthread_delete(dthread_t *dt)
  70. {
  71.     list_remove(&dt->link);
  72.     free(dt);
  73. }
  74.  
  75. /*
  76.  * Get dthread struct serviced by fibril fid.
  77.  */
  78. dthread_t *dthread_get_by_fid(fid_t fid)
  79. {
  80.     link_t *cur;
  81.     dthread_t *dt;
  82.  
  83.     dt = NULL;
  84.     for (cur = dthreads.next; cur != &dthreads; cur = cur->next) {
  85.         dt = list_get_instance(cur, dthread_t, link);
  86.         if (dt->fid == fid) return dt;
  87.     }  
  88.  
  89.     return NULL;
  90. }
  91.  
  92. /*
  93.  * Get dthread struct for dthread serviced by the current fibril.
  94.  */
  95. dthread_t *dthread_get(void)
  96. {
  97.     return dthread_get_by_fid(fibril_get_id());
  98. }
  99.  
  100. void dthread_stop_me(void)
  101. {
  102.     dthread_t *dt;
  103.     int i;
  104.  
  105.     dt = dthread_get();
  106.     assert(dt);
  107.  
  108.     futex_down(&async_futex);
  109.  
  110.     dt->stopped = 1;
  111.     fibril_switch(FIBRIL_TO_MANAGER);
  112.  
  113.     futex_up(&async_futex);
  114. }
  115.  
  116. void dthread_resume(dthread_t *dt)
  117. {
  118.     if (!dt->stopped) return;
  119.  
  120.     futex_down(&async_futex);
  121.  
  122.     fibril_add_ready(dt->fid);
  123.     dt->stopped = 0;
  124.  
  125.     futex_up(&async_futex);
  126. }
  127.  
  128.  
  129. /** @}
  130.  */
  131.