Subversion Repositories HelenOS

Rev

Rev 2941 | Rev 3100 | 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 "include/arch.h"
  42. #include "main.h"
  43. #include "dthread.h"
  44.  
  45. static int last_thread_id = 0;
  46.  
  47. link_t dthreads;        /* List of application's threads */
  48. dthread_t *cwt;         /* Current working thread */
  49.  
  50. dthread_t *dthread_new(thash_t hash)
  51. {
  52.     dthread_t *dt;
  53.  
  54.     dt = malloc(sizeof(dthread_t));
  55.     if (!dt) {
  56.         exit(1);
  57.     }
  58.  
  59.     dt->id = ++last_thread_id;
  60.     dt->hash = hash;
  61.     dt->fid = 0;
  62.     dt->stopped = 0;
  63.  
  64.     if (!cwt) cwt = dt;
  65.  
  66.     list_append(&dt->link, &dthreads);
  67.  
  68.     return dt;
  69. }
  70.  
  71. void dthread_delete(dthread_t *dt)
  72. {
  73.     list_remove(&dt->link);
  74.     free(dt);
  75. }
  76.  
  77. /*
  78.  * Get dthread struct serviced by fibril fid.
  79.  */
  80. dthread_t *dthread_get_by_fid(fid_t fid)
  81. {
  82.     link_t *cur;
  83.     dthread_t *dt;
  84.  
  85.     dt = NULL;
  86.     for (cur = dthreads.next; cur != &dthreads; cur = cur->next) {
  87.         dt = list_get_instance(cur, dthread_t, link);
  88.         if (dt->fid == fid) return dt;
  89.     }  
  90.  
  91.     return NULL;
  92. }
  93.  
  94. /*
  95.  * Get dthread struct for dthread serviced by the current fibril.
  96.  */
  97. dthread_t *dthread_get(void)
  98. {
  99.     return dthread_get_by_fid(fibril_get_id());
  100. }
  101.  
  102. void dthread_stop_me(void)
  103. {
  104.     dthread_t *dt;
  105.  
  106.     dt = dthread_get();
  107.     assert(dt);
  108.  
  109.     futex_down(&async_futex);
  110.  
  111.     dt->stopped = 1;
  112.     fibril_switch(FIBRIL_TO_MANAGER);
  113.  
  114.     futex_up(&async_futex);
  115. }
  116.  
  117. void dthread_resume(dthread_t *dt)
  118. {
  119.     if (!dt->stopped) return;
  120.  
  121.     futex_down(&async_futex);
  122.  
  123.     fibril_add_ready(dt->fid);
  124.     dt->stopped = 0;
  125.  
  126.     futex_up(&async_futex);
  127. }
  128.  
  129. unsigned dthread_get_pc(dthread_t *dt)
  130. {
  131.     static istate_t istate;
  132.     int rc;
  133.  
  134.     rc = udebug_regs_read(app_phone, dt->hash, &istate);
  135.     if (rc < 0) {
  136.         printf("failed reading registers (%d)\n", rc);
  137.         return 0;
  138.     }
  139.  
  140.     return istate_get_pc(&istate);
  141. }
  142.  
  143. /** @}
  144.  */
  145.