Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 51c70da7f5
Fetching contributors…

Cannot retrieve contributors at this time

file 156 lines (124 sloc) 4.262 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
/* Copyright (C) 2006, Ephemeral Security, LLC
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License, version 2.1
* as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include "waspvm.h"

wasp_symbol wasp_ss_timeout;

/*

void wasp_get_now( wasp_quad* secs, wasp_quad* nsecs ){
struct timespec ts;
clock_gettime( CLOCK_REALTIME, &ts );
*secs = ts.tv_sec;
*nsecs = ts.tv_nsec;
}

*/

void wasp_task_cb( int fd, short evt, void* context ){
    wasp_task task = (wasp_task) context;
    
    if( ! task->pending ) return;
    int ms = task->task_mt( task );

    if( ms ){
        task->time.tv_sec = ms / 1000;
        task->time.tv_usec = ((ms % 1000)) * 1000;
        evtimer_add( &( task->event ), &( task->time ) );
    }else{
        task->pending = 0;
        wasp_disable_os_loop( );
        wasp_unroot_obj( (wasp_object) task );
    };
}

wasp_task wasp_make_task( wasp_task_mt mt, wasp_value context ){
    wasp_task task = WASP_OBJALLOC( task );
    evtimer_set( &( task->event ), wasp_task_cb, task );
    task->task_mt = mt;
    task->context = context;
    task->pending = 0;
    return task;
}

wasp_task wasp_schedule_task( wasp_task task, wasp_quad ms ){
    if( task->pending ) return;
    task->time.tv_sec = ms / 1000;
    task->time.tv_usec = ((ms % 1000)) * 1000;
    evtimer_add( &( task->event ), &( task->time ) );
    task->pending = 1;
    wasp_root_obj( (wasp_object) task );
    wasp_enable_os_loop( );
}

void wasp_cancel_task( wasp_task task ){
    if( ! task->pending ) return;
    evtimer_del( &( task->event ) );
    wasp_disable_os_loop( );
    wasp_unroot_obj( (wasp_object) task );
    task->pending = 0;
}

void wasp_trace_task( wasp_task task ){
    wasp_grey_val( task->context );
}

WASP_GENERIC_COMPARE( task );
WASP_GENERIC_FORMAT( task );
WASP_GENERIC_FREE( task );
WASP_C_TYPE( task )

int wasp_timeout_mt( wasp_task task ){
    wasp_input input = wasp_input_fv( task->context );
    
    if( wasp_input_monitored( input ) ){
        wasp_wake_monitor( input, wasp_vf_symbol( wasp_ss_timeout ) );
    };
    
    return 0;
}

WASP_BEGIN_PRIM( "timeout", timeout )
    REQ_INTEGER_ARG( ms );
    REQ_INPUT_ARG( input );
    NO_REST_ARGS( );
    
    wasp_task task = wasp_make_task( wasp_timeout_mt, wasp_vf_input( input ) );
    
    if( ms < 0 ) wasp_errf( wasp_es_vm, "si", "Negative timeouts are not allowed", ms );

    wasp_schedule_task( task, ms );
    TASK_RESULT( task );
WASP_END_PRIM( timeout )

WASP_BEGIN_PRIM( "cancel-timeout", cancel_timeout )
    REQ_TASK_ARG( task )
    NO_REST_ARGS( );
    
    wasp_cancel_task( task );
    
    NO_RESULT( );
WASP_END_PRIM( cancel_task )

int wasp_pause_mt( wasp_task task ){
    wasp_enable_process( wasp_process_fv( task->context ) );
    return 0;
}

WASP_BEGIN_PRIM( "pause", pause )
    wasp_value data;

    OPT_INTEGER_ARG( ms );
    NO_REST_ARGS( );

    // The following statement ensures that pause will not be called again
    // when we return.
    WASP_CP = WASP_CP->cp;

    // No result for when we return.
    WASP_RX = wasp_vf_null( );

    wasp_process p = wasp_active_process;
    wasp_disable_process( p );
    if( ! has_ms ){
        wasp_enable_process( p );
    }else{
        wasp_task task = wasp_make_task( wasp_pause_mt, wasp_vf_process( p ) );
        wasp_schedule_task( task, ms );
    }

    // Restart the process loop since the active has been suspended. Does
    // not return.
    wasp_proc_loop( );
WASP_END_PRIM( pause )

void wasp_init_time_subsystem( ){
    WASP_I_TYPE( task );
    wasp_ss_timeout = wasp_symbol_fs( "timeout" );

    WASP_BIND_PRIM( timeout );
    WASP_BIND_PRIM( cancel_timeout );
    WASP_BIND_PRIM( pause );
}
Something went wrong with that request. Please try again.