Skip to content

Instantly share code, notes, and snippets.

@aerodame
Last active August 29, 2015 14:20
Show Gist options
  • Save aerodame/beb3845611c107901f3e to your computer and use it in GitHub Desktop.
Save aerodame/beb3845611c107901f3e to your computer and use it in GitHub Desktop.
ThreadOS Kernel Snippet
import java.util.*;
import java.lang.reflect.*;
import java.io.*;
public class Kernel {
// Interrupt requests
public final static int INTERRUPT_SOFTWARE = 1; // System calls
public final static int INTERRUPT_DISK = 2; // Disk interrupts
public final static int INTERRUPT_IO = 3; // Other I/O interrupts
// System calls
public final static int BOOT = 0; // SysLib.boot( )
...
public final static int RAWREAD = 5; // SysLib.rawread(int blk, byte b[])
public final static int RAWWRITE= 6; // SysLib.rawwrite(int blk, byte b[])
public final static int SYNC = 7; // SysLib.sync( )
// Return values
public final static int OK = 0;
public final static int ERROR = -1;
// System thread references
private static Scheduler scheduler;
private static Disk disk;
// The heart of Kernel
public static int interrupt( int irq, int cmd, int param, Object args ) {
TCB myTcb;
switch( irq ) {
case INTERRUPT_SOFTWARE: // System calls
switch( cmd ) {
case BOOT:
// instantiate and start a scheduler
scheduler = new Scheduler( );
scheduler.start( );
// instantiate and start a disk
disk = new Disk( 100 );
disk.start( );
return OK;
...
case RAWREAD: // read a block of data from disk
while ( disk.read( param, ( byte[] )args ) == false )
; // busy wait
while ( disk.testAndResetReady( ) == false )
; // another busy wait
return OK;
case RAWWRITE: // write a block of data to disk
while ( disk.write( param, ( byte[] )args ) == false )
; // busy wait
while ( disk.testAndResetReady( ) == false )
; // another busy wait
return OK;
case SYNC: // synchronize disk data to a real file
while ( disk.sync( ) == false )
; // busy wait
while ( disk.testAndResetReady( ) == false )
; // another busy wait
return OK;
}
return ERROR;
case INTERRUPT_DISK: // Disk interrupts
case INTERRUPT_IO: // other I/O interrupts (not implemented)
}
return OK;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment