Skip to content

Commit

Permalink
Added Win32 mutex implementation, made os/mmap portable, made closure…
Browse files Browse the repository at this point in the history
…s work under Win32
  • Loading branch information
nddrylliog committed Jun 13, 2010
1 parent f07565d commit c5980c2
Show file tree
Hide file tree
Showing 8 changed files with 335 additions and 238 deletions.
54 changes: 37 additions & 17 deletions sdk/internals/yajit/BinarySeq.ooc
Expand Up @@ -6,60 +6,80 @@ errno: extern Int
strerror: extern func (Int) -> String strerror: extern func (Int) -> String


BinarySeq: class { BinarySeq: class {

data : UChar* data : UChar*
size : SizeT size : SizeT
index := 0 index := 0
transTable := HashMap<String, Int> new() transTable := HashMap<String, Int> new()

init: func ~withData (=size, d: UChar*) { init: func ~withData (=size, d: UChar*) {
init(size) init(size)
index = size index = size
memcpy(data, d, size * UChar size) memcpy(data, d, size * UChar size)
} }

init: func ~withSize (=size) { init: func ~withSize (=size) {
memsize := size * UChar size memsize := size * UChar size
// at least 4096, and a multiple of 4096 that is bigger than memsize // at least 4096, and a multiple of 4096 that is bigger than memsize
realsize := memsize + 4096 - (memsize % 4096) realsize := memsize + 4096 - (memsize % 4096)
data = gc_malloc(realsize) data = gc_malloc(realsize)
result := mprotect(data, realsize, PROT_READ | PROT_WRITE | PROT_EXEC)
if(result != 0) { version (linux || apple) {
printf("mprotect(%p, %zd) failed with code %d. Message = %s\n", data, realsize, result, strerror(errno)) result := mprotect(data, realsize, PROT_READ | PROT_WRITE | PROT_EXEC)
if(result != 0) {
Exception new(This, "mprotect(%p, %zd) failed with code %d. Message = %s\n" format(data, realsize, result, strerror(errno))) throw()
}
} }

version (windows) {
// TODO: it seems that it works fine under Windows XP+mingw+GCC4.5 without it
// on the other hand, it returns false with it and throws the exception.
// Does anyone have any clue what's going on? We can't use VirtualAlloc because
// it would leak :x

/*
result := VirtualProtect(data, realsize, PAGE_READWRITE, null)
if(!result) {
Exception new(This, "VirtualProtect(%p, %zd) failed !\n" format(data, realsize)) throw()
}
*/
}

version (!(linux || apple || windows)) {
Exception new(This, "Closures aren't supported on your platform (yet) !") throw()
}

initTransTable() initTransTable()
// mmap is leaking (cause we don't know when to free), and apparently not needed, but just in case, here's the correct call
//data = mmap(null, memsize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_LOCKED | MAP_ANONYMOUS, -1, 0)
} }

initTransTable: func { initTransTable: func {
transTable["c"] = Char size transTable["c"] = Char size
transTable["d"] = Double size transTable["d"] = Double size
transTable["f"] = Float size transTable["f"] = Float size
transTable["h"] = Short size transTable["h"] = Short size
transTable["i"] = Int size transTable["i"] = Int size
transTable["l"] = Long size transTable["l"] = Long size
transTable["P"] = Pointer size transTable["P"] = Pointer size
} }


append: func ~other (other: BinarySeq) -> BinarySeq { append: func ~other (other: BinarySeq) -> BinarySeq {
append(other data, other size) append(other data, other size)
} }

append: func ~withLength (ptr: Pointer, ptrLength: SizeT) -> BinarySeq { append: func ~withLength (ptr: Pointer, ptrLength: SizeT) -> BinarySeq {
memcpy(data + index, ptr, ptrLength) memcpy(data + index, ptr, ptrLength)
index += ptrLength index += ptrLength
return this return this
} }

reset: func { index = 0 } reset: func { index = 0 }

print: func { print: func {
for(i : Int in 0..index) for(i : Int in 0..index)
printf("%.2x ", data[i]) printf("%.2x ", data[i])
println() println()
} }

} }


operator += (b1, b2 : BinarySeq) -> BinarySeq { operator += (b1, b2 : BinarySeq) -> BinarySeq {
Expand Down
260 changes: 131 additions & 129 deletions sdk/os/Time.ooc
Expand Up @@ -10,154 +10,156 @@ version(!linux) {
} }


version(windows) { version(windows) {
include windows include windows
} }


/* covers & functions */ /* covers & functions */


version(windows) { version(windows) {
SystemTime: cover from SYSTEMTIME { SystemTime: cover from SYSTEMTIME {
wHour, wMinute, wSecond, wMilliseconds : extern UShort wHour, wMinute, wSecond, wMilliseconds : extern UShort
} }


timeGetTime: extern func -> UInt32 timeGetTime: extern func -> UInt32
GetLocalTime: extern func (SystemTime*) GetLocalTime: extern func (SystemTime*)
Sleep: extern func (UInt) Sleep: extern func (UInt)
} }


version(!windows) { version(!windows) {
TimeT: cover from time_t TimeT: cover from time_t
TimeZone: cover from struct timezone TimeZone: cover from struct timezone
TMStruct: cover from struct tm { TMStruct: cover from struct tm {
tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday, tm_isdst : extern Int tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday, tm_isdst : extern Int
} }
TimeVal: cover from struct timeval { TimeVal: cover from struct timeval {
tv_sec: extern TimeT tv_sec: extern TimeT
tv_usec: extern Int tv_usec: extern Int
} }


time: extern proto func (TimeT*) -> TimeT time: extern proto func (TimeT*) -> TimeT
localtime: extern func (TimeT*) -> TMStruct* localtime: extern func (TimeT*) -> TMStruct*
gettimeofday: extern func (TimeVal*, TimeZone*) -> Int gettimeofday: extern func (TimeVal*, TimeZone*) -> Int
usleep: extern func (UInt) usleep: extern func (UInt)
} }


/* implementation */ /* implementation */


Time: class { Time: class {
__time_millisec_base := static This runTime __time_millisec_base := static This runTime


/** /**
Returns the microseconds that have elapsed in the current minute. Returns the microseconds that have elapsed in the current minute.
*/ */
microtime: static func -> LLong { microtime: static func -> LLong {
return microsec() as LLong + (sec() as LLong) * 1_000_000 return microsec() as LLong + (sec() as LLong) * 1_000_000
} }


/** /**
Returns the microseconds that have elapsed in the current second. Returns the microseconds that have elapsed in the current second.
*/ */
microsec: static func -> UInt { microsec: static func -> UInt {
version(windows) { version(windows) {
st: SystemTime st: SystemTime
GetLocalTime(st&) GetLocalTime(st&)
return st wMilliseconds * 1000 return st wMilliseconds * 1000
} }
version(!windows) { version(!windows) {
tv : TimeVal tv : TimeVal
gettimeofday(tv&, null) gettimeofday(tv&, null)
return tv tv_usec return tv tv_usec
} }
return -1 return -1
} }


/** /**
Gets the number of milliseconds elapsed since program start. Gets the number of milliseconds elapsed since program start.
*/ */
runTime: static UInt { runTime: static UInt {
get { get {
t: ULLong t: ULLong
version(windows) { version(windows) {
// NOTE: timeGetTime only returns a 32-bit integer. the upside is // NOTE: timeGetTime only returns a 32-bit integer. the upside is
// that it's accurate to 1ms, but unfortunately rollover is very // that it's accurate to 1ms, but unfortunately rollover is very
// possible // possible
timeGetTime() as UInt - __time_millisec_base
} // FIXME: getting undefined reference for now :x
//timeGetTime() as UInt - __time_millisec_base
}
version(!windows) { version(!windows) {
tv : TimeVal tv : TimeVal
gettimeofday(tv&, null) gettimeofday(tv&, null)
return ((tv tv_usec / 1000 + tv tv_sec * 1000) - __time_millisec_base) as UInt return ((tv tv_usec / 1000 + tv tv_sec * 1000) - __time_millisec_base) as UInt
} }
return -1 return -1
} }
} }

/** /**
Returns the seconds that have elapsed in the current minute. Returns the seconds that have elapsed in the current minute.
*/ */
sec: static func -> UInt { sec: static func -> UInt {
version(windows) { version(windows) {
st: SystemTime st: SystemTime
GetLocalTime(st&) GetLocalTime(st&)
return st wSecond return st wSecond
} }
version(!windows) { version(!windows) {
tt := time(null) tt := time(null)
val := localtime(tt&) val := localtime(tt&)
return val@ tm_sec return val@ tm_sec
} }
return -1 return -1
} }

/** /**
Returns the minutes that have elapsed in the current hour. Returns the minutes that have elapsed in the current hour.
*/ */
min: static func -> UInt { min: static func -> UInt {
version(windows) { version(windows) {
st: SystemTime st: SystemTime
GetLocalTime(st&) GetLocalTime(st&)
return st wMinute return st wMinute
} }
version(!windows) { version(!windows) {
tt := time(null) tt := time(null)
val := localtime(tt&) val := localtime(tt&)
return val@ tm_min return val@ tm_min
} }
return -1 return -1
} }

/** /**
Returns the hours that have elapsed in the current day. Returns the hours that have elapsed in the current day.
*/ */
hour: static func -> UInt { hour: static func -> UInt {
version(windows) { version(windows) {
st: SystemTime st: SystemTime
GetLocalTime(st&) GetLocalTime(st&)
return st wHour return st wHour
} }
version(!windows) { version(!windows) {
tt := time(null) tt := time(null)
val := localtime(tt&) val := localtime(tt&)
return val@ tm_hour return val@ tm_hour
} }
return -1 return -1
} }

sleepSec: static func (duration: Float) { sleepSec: static func (duration: Float) {
sleepMicro(duration * 1_000_000) sleepMicro(duration * 1_000_000)
} }


sleepMilli: static func (duration: UInt) { sleepMilli: static func (duration: UInt) {
sleepMicro(duration * 1_000) sleepMicro(duration * 1_000)
} }


sleepMicro: static func (duration: UInt) { sleepMicro: static func (duration: UInt) {
version(windows) { version(windows) {
Sleep(duration / 1_000) Sleep(duration / 1_000)
} }
version(!windows) { version(!windows) {
usleep(duration) usleep(duration)
} }
} }


} }

0 comments on commit c5980c2

Please sign in to comment.