int do_test_sem(const char *test_parm) { (void) test_parm; printf("Testing semaphores\n"); hal_sem_init(&test_sem_0, "semTest"); softirq = hal_alloc_softirq(); if( softirq < 0 ) test_fail_msg( 1, "Unable to get softirq" ); hal_set_softirq_handler( softirq, sem_softirq, 0 ); //int tid = hal_start_kernel_thread_arg( sem_rel, 0 ); printf("sema wait 1\n"); // Direct sem_released = 0; hal_sem_acquire( &test_sem_0 ); test_check_eq(sem_released,1); hal_sleep_msec( 100 ); printf("sema wait 2\n"); #if TEST_SOFTIRQ // Softirq sem_released = 0; hal_sem_acquire( &test_sem_0 ); test_check_eq(sem_released,1); #endif hal_sleep_msec( 100 ); printf("sema timeout\n"); sem_released = 0; hal_start_kernel_thread_arg( sem_etc, 0 ); hal_sleep_msec( 100 ); test_check_eq(sem_released,0); hal_sleep_msec( 120 ); test_check_eq(sem_released,1); test_check_eq( rc, ETIMEDOUT); stop_sem_test = 1; printf("Done testing semaphores\n"); return 0; }
phantom_device_t * driver_isa_com_probe( int port, int irq, int stage ) { (void) stage; if( !com_detect( seq_number, port) ) return 0; phantom_device_t * dev = calloc(sizeof(phantom_device_t), 1); dev->iobase = port; dev->irq = irq; dev->name = "com"; dev->seq_number = seq_number++; dev->dops.start = com_start; dev->dops.stop = com_stop; dev->dops.read = com_read; dev->dops.write = com_write; if( hal_irq_alloc( irq, &com_interrupt, dev, HAL_IRQ_SHAREABLE ) ) { SHOW_ERROR( 0, "IRQ %d is busy", irq ); free(dev); return 0; } com_port_t *cp = calloc( sizeof(com_port_t), 1 ); dev->drv_private = cp; hal_sem_init( &(cp->rsem), "serialRd" ); hal_sem_init( &(cp->wsem), "serialWr" ); cp->rdq = wtty_init(); assert(cp->rdq); cp->wrq = wtty_init(); assert(cp->wrq); cp->baudRate = 9600; cp->stopBits = 1; cp->dataBits = 8; cp->parity = 0; com_setbaud(dev, cp->baudRate); hal_start_kernel_thread_arg( com_rd_thread, dev ); hal_start_kernel_thread_arg( com_wr_thread, dev ); return dev; }
int net_timer_init(void) { int err; net_q.next = net_q.prev = (net_timer_event *)&net_q; err = hal_mutex_init(&net_q.lock, "net timer mutex"); if(err < 0) return err; //net_q.wait_sem = sem_create(0, "net timer wait sem"); if( hal_sem_init(&(net_q.wait_sem), "NetQ") < 0 ) { mutex_destroy(&net_q.lock); return -1; //net_q.wait_sem; } //net_q.runner_thread = thread_create_kernel_thread("net timer runner", &net_timer_runner, NULL); net_q.runner_thread = hal_start_kernel_thread_arg( &net_timer_runner, NULL ); if(net_q.runner_thread < 0) { sem_delete(net_q.wait_sem); mutex_destroy(&net_q.lock); return -1; //net_q.runner_thread; } //thread_resume_thread(net_q.runner_thread); return 0; }
static errno_t threads_test() { hal_cond_init(&c, "threadTest"); hal_mutex_init(&m, "threadTest"); hal_sem_init(&s, "threadTest"); int i = 40; n_t_empty = i; while(i-- > 0) phantom_create_thread( t_empty, "Empty", 0 ); pressEnter("will create thread"); phantom_create_thread( thread1, "__T1__", 0 ); phantom_create_thread( thread1, "__T2__", 0 ); //phantom_create_thread( thread1, "__T3__" ); //phantom_create_thread( t_wait, "__TW__" ); int tid = hal_start_kernel_thread_arg( t_wait, "__TW__" ); i = 40; while(i-- > 0) { if(TEST_CHATTY) pressEnter("will yield"); YIELD(); if(TEST_CHATTY) printf("!! back in main\n"); } t_kill_thread( tid ); hal_sleep_msec( 30 ); thread_stop_request = 1; hal_sleep_msec( 10 ); thread_activity_counter = 0; hal_sleep_msec( 1000 ); if( thread_activity_counter ) { SHOW_ERROR0( 0, "Can't stop thread" ); return -1; } while(n_t_empty > 0) { SHOW_FLOW( 0, "wait for %d threads", n_t_empty ); hal_sleep_msec(500); } if(p._ah.refCount != 1) { SHOW_ERROR( 0, "p._ah.refCount = %d", p._ah.refCount ); test_fail_msg( -1, "refcount" ); } else SHOW_ERROR( 0, "p._ah.refCount = %d, SUCCESS", p._ah.refCount ); return 0; }
int do_test_01_threads(const char *test_parm) { (void) test_parm; printf("Testing thread state integrity\n"); hal_start_kernel_thread_arg( thread_ones, 0 ); hal_start_kernel_thread_arg( thread_zeroes, 0 ); printf("Wait for 01 threads to finish\n"); while(zo_run) hal_sleep_msec(100); if(zo_fail) test_fail_msg( -1, "data corruption" ); return 0; }
errno_t lookup_fs(phantom_disk_partition_t *p) { char pname[128]; partGetName( p, pname, sizeof(pname) ); SHOW_INFO( 0, "Look for filesystems on partition %s", pname ); unsigned int i; for( i = 0; i < sizeof(fs_drivers)/sizeof(fs_probe_t); i++ ) { fs_probe_t *fp = &fs_drivers[i]; SHOW_INFO( 0, "probe %s fs on %s", fp->name, pname ); errno_t ret = fp->probe_f( p ); if( ret ) continue; SHOW_INFO( 0, "%s file sysem found on partition %s", fp->name, pname ); if(!fp->use_f) { SHOW_ERROR( 0, "%s file sysem is not implemented yet", fp->name ); continue; } #if FS_START_THREAD // BUG HACK - activate phantom fs syncronously, or else we will die attempting to use it if(fp->use_f == fs_use_phantom) fp->use_f( p ); else hal_start_kernel_thread_arg( (void (*)(void *))fp->use_f, p ); #else ret = fp->use_f( p ); if( ret ) { SHOW_ERROR( 0, "%s file sysem driver rejected partition %s", fp->name, pname ); continue; } SHOW_INFO( 0, "%s file sysem driver occupies partition %s", fp->name, pname ); #endif return 0; } return EINVAL; }
errno_t auto_mount( const char *name, uufs_t *fs, char *out_mnt_path, size_t outsz, int flags ) { static int am_index = 0; static char mpath[32]; snprintf( mpath, sizeof(mpath), "/amnt%d", am_index ++ ); SHOW_INFO( 0, "Partition %s mounted @ %s", name, mpath ); if( outsz && out_mnt_path ) strlcpy( out_mnt_path, mpath, outsz ); errno_t rc = add_mount( mpath, name, fs ); if( !rc && (flags & AUTO_MOUNT_FLAG_AUTORUN) ) hal_start_kernel_thread_arg( auto_run_thread, strdup(mpath) ); return rc; }
void if_simple_setup(ifnet *interface, int addr, int netmask, int bcast, int net, int router, int def_router) { ifaddr *address; //addr = htonl(addr); //netmask = htonl(netmask); //bcast = htonl(bcast); // set the ip address for this net interface address = malloc(sizeof(ifaddr)); address->addr.len = 4; address->addr.type = ADDR_TYPE_IP; //NETADDR_TO_IPV4(address->addr) = htonl(addr); NETADDR_TO_IPV4(address->addr) = addr; address->netmask.len = 4; address->netmask.type = ADDR_TYPE_IP; //NETADDR_TO_IPV4(address->netmask) = htonl(netmask); NETADDR_TO_IPV4(address->netmask) = netmask; address->broadcast.len = 4; address->broadcast.type = ADDR_TYPE_IP; //NETADDR_TO_IPV4(address->broadcast) = htonl(bcast); NETADDR_TO_IPV4(address->broadcast) = bcast; if_bind_address(interface, address); #if 1 printf("if a "); dump_ipv4_addr(addr); printf(" mask "); dump_ipv4_addr(netmask); printf(" broad "); dump_ipv4_addr(bcast); printf("\n"); #endif // set up an initial routing table int rc; if( (rc = ipv4_route_add( net, netmask, router, interface->id) ) ) { SHOW_ERROR( 1, "Adding route - failed, rc = %d", rc); } else { SHOW_INFO0( 2, "Adding route - ok"); } SHOW_INFO0( 2, "Adding default route..."); if( (rc = ipv4_route_add_default( router, interface->id, def_router ) ) ) { SHOW_ERROR( 1, "Adding route - failed, rc = %d", rc); } else { SHOW_INFO0( 2, "Adding route - ok"); } #if 0 hal_start_kernel_thread_arg( bootp_thread, interface ); #else // Now try to get something real :) bootp(interface); #endif }