Subversion Repositories HelenOS-historic

Rev

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

  1. /*
  2.  * Copyright (C) 2006 Ondrej Palkovsky
  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. #include <libadt/list.h>
  30. #include <psthread.h>
  31. #include <malloc.h>
  32. #include <unistd.h>
  33. #include <thread.h>
  34. #include <stdio.h>
  35. #include <kernel/arch/faddr.h>
  36. #include <futex.h>
  37. #include <assert.h>
  38. #include <async.h>
  39.  
  40. #ifndef PSTHREAD_INITIAL_STACK_PAGES_NO
  41. #define PSTHREAD_INITIAL_STACK_PAGES_NO 1
  42. #endif
  43.  
  44. static LIST_INITIALIZE(ready_list);
  45. static LIST_INITIALIZE(manager_list);
  46.  
  47. static void psthread_exit(void) __attribute__ ((noinline));
  48. static void psthread_main(void);
  49.  
  50. static atomic_t psthread_futex = FUTEX_INITIALIZER;
  51.  
  52. /** Setup PSthread information into TCB structure */
  53. psthread_data_t * psthread_setup()
  54. {
  55.     psthread_data_t *pt;
  56.     tcb_t *tcb;
  57.  
  58.     tcb = __make_tls();
  59.     if (!tcb)
  60.         return NULL;
  61.  
  62.     pt = malloc(sizeof(*pt));
  63.     if (!pt) {
  64.         __free_tls(tcb);
  65.         return NULL;
  66.     }
  67.  
  68.     tcb->pst_data = pt;
  69.     pt->tcb = tcb;
  70.  
  71.     return pt;
  72. }
  73.  
  74. void psthread_teardown(psthread_data_t *pt)
  75. {
  76.     __free_tls(pt->tcb);
  77.     free(pt);
  78. }
  79.  
  80. /** Function to preempt to other pseudo thread without adding
  81.  * currently running pseudo thread to ready_list.
  82.  */
  83. void psthread_exit(void)
  84. {
  85.     psthread_data_t *pt;
  86.  
  87.     futex_down(&psthread_futex);
  88.  
  89.     if (!list_empty(&ready_list))
  90.         pt = list_get_instance(ready_list.next, psthread_data_t, link);
  91.     else if (!list_empty(&manager_list))
  92.         pt = list_get_instance(manager_list.next, psthread_data_t, link);
  93.     else {
  94.         printf("Cannot find suitable psthread to run.\n");
  95.         _exit(0);
  96.     }
  97.     list_remove(&pt->link);
  98.     futex_up(&psthread_futex);
  99.  
  100.     context_restore(&pt->ctx);
  101.     /* Never reached */
  102. }
  103.  
  104. /** Function that is called on entry to new uspace thread */
  105. void psthread_main(void)
  106. {
  107.     psthread_data_t *pt = __tcb_get()->pst_data;
  108.  
  109.     pt->retval = pt->func(pt->arg);
  110.  
  111.     pt->finished = 1;
  112.     if (pt->waiter)
  113.         list_append(&pt->waiter->link, &ready_list);
  114.  
  115.     psthread_exit();
  116. }
  117.  
  118. /** Schedule next userspace pseudo thread.
  119.  *
  120.  * @param tomanager If true, we are switching to next ready manager thread
  121.  *                  (if none is found, thread is exited)
  122.  * @param frommanager If true, we are switching from manager thread
  123.  * @return 0 if there is no ready pseudo thread, 1 otherwise.
  124.  */
  125. int psthread_schedule_next_adv(pschange_type ctype)
  126. {
  127.     psthread_data_t *pt;
  128.     int retval = 0;
  129.    
  130.     futex_down(&psthread_futex);
  131.  
  132.     if (ctype == PS_PREEMPT && list_empty(&ready_list))
  133.         goto ret_0;
  134.  
  135.     if (ctype == PS_FROM_MANAGER && list_empty(&ready_list)) {
  136.         goto ret_0;
  137.     }
  138.     /* If we are going to manager and none exists, create it */
  139.     if (ctype == PS_TO_MANAGER && list_empty(&manager_list))
  140.         async_create_manager();
  141.  
  142.     pt = __tcb_get()->pst_data;
  143.     if (!context_save(&pt->ctx))
  144.         return 1; // futex_up already done here
  145.  
  146.     if (ctype == PS_PREEMPT)
  147.         list_append(&pt->link, &ready_list);
  148.     else if (ctype == PS_FROM_MANAGER)
  149.         list_append(&pt->link, &manager_list);
  150.    
  151.     if (ctype == PS_TO_MANAGER)
  152.         pt = list_get_instance(manager_list.next,psthread_data_t, link);
  153.     else
  154.         pt = list_get_instance(ready_list.next, psthread_data_t, link);
  155.     list_remove(&pt->link);
  156.  
  157.     futex_up(&psthread_futex);
  158.     context_restore(&pt->ctx);
  159.  
  160. ret_0:
  161.     futex_up(&psthread_futex);
  162.     return retval;
  163. }
  164.  
  165. /** Wait for uspace pseudo thread to finish.
  166.  *
  167.  * @param psthrid Pseudo thread to wait for.
  168.  *
  169.  * @return Value returned by the finished thread.
  170.  */
  171. int psthread_join(pstid_t psthrid)
  172. {
  173.     volatile psthread_data_t *pt, *mypt;
  174.     volatile int retval;
  175.  
  176.     /* Handle psthrid = Kernel address -> it is wait for call */
  177.     pt = (psthread_data_t *) psthrid;
  178.  
  179.     if (!pt->finished) {
  180.         mypt = __tcb_get()->pst_data;
  181.         if (context_save(&((psthread_data_t *) mypt)->ctx)) {
  182.             pt->waiter = (psthread_data_t *) mypt;
  183.             psthread_exit();
  184.         }
  185.     }
  186.     retval = pt->retval;
  187.  
  188.     free(pt->stack);
  189.     psthread_teardown((void *)pt);
  190.  
  191.     return retval;
  192. }
  193.  
  194. /**
  195.  * Create a userspace thread
  196.  *
  197.  * @param func Pseudo thread function.
  198.  * @param arg Argument to pass to func.
  199.  *
  200.  * @return 0 on failure, TLS of the new pseudo thread.
  201.  */
  202. pstid_t psthread_create(int (*func)(void *), void *arg)
  203. {
  204.     psthread_data_t *pt;
  205.  
  206.     pt = psthread_setup();
  207.     if (!pt)
  208.         return 0;
  209.     pt->stack = (char *) malloc(PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize());
  210.  
  211.     if (!pt->stack) {
  212.         psthread_teardown(pt);
  213.         return 0;
  214.     }
  215.  
  216.     pt->arg= arg;
  217.     pt->func = func;
  218.     pt->finished = 0;
  219.     pt->waiter = NULL;
  220.  
  221.     context_save(&pt->ctx);
  222.     context_set(&pt->ctx, FADDR(psthread_main), pt->stack, PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize(),
  223.             pt->tcb);
  224.  
  225.     return (pstid_t )pt;
  226. }
  227.  
  228. /** Add a thread to ready list */
  229. void psthread_add_ready(pstid_t psthrid)
  230. {
  231.     psthread_data_t *pt;
  232.  
  233.     pt = (psthread_data_t *) psthrid;
  234.     futex_down(&psthread_futex);
  235.     list_append(&pt->link, &ready_list);
  236.     futex_up(&psthread_futex);
  237. }
  238.  
  239. /** Add a thread to manager list */
  240. void psthread_add_manager(pstid_t psthrid)
  241. {
  242.     psthread_data_t *pt;
  243.  
  244.     pt = (psthread_data_t *) psthrid;
  245.  
  246.     futex_down(&psthread_futex);
  247.     list_append(&pt->link, &manager_list);
  248.     futex_up(&psthread_futex);
  249. }
  250.  
  251. /** Remove one manager from manager list */
  252. void psthread_remove_manager()
  253. {
  254.     futex_down(&psthread_futex);
  255.     if (list_empty(&manager_list)) {
  256.         futex_up(&psthread_futex);
  257.         return;
  258.     }
  259.     list_remove(manager_list.next);
  260.     futex_up(&psthread_futex);
  261. }
  262.