Skip to content
Permalink
Browse files
Added data io streams for arbitrary data read/write. Added array io s…
…treams for array read/write. Deleted overarching thread code and reverted see.concurrent.Thread to its original state.
  • Loading branch information
James King committed Aug 11, 2013
1 parent 2a94714 commit f1224cf
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 95 deletions.
@@ -167,79 +167,6 @@ function StandardGlobals.STR(vm, str)
return vm.base.String.new(str)
end

ThreadPool = { }
ThreadPool.__index = ThreadPool

--[[
Creates a new ThreadPool.
]]
function ThreadPool.new()
local self = { }
setmetatable(self, ThreadPool)
self.threads = { }
self.current = { }
self.eventFilters = { }
self.eventData = { }
return self
end

--[[
Runs a thread in this ThreadPool.
]]
function ThreadPool:runThread(i)
self.current = self.threads[i]
local co = self.threads[i].co

if not self.eventFilters[co] or self.eventFilters[co] == self.eventData[1] or self.eventData[1] == "terminate" then
local ok, param = coroutine.resume(co, unpack(self.eventData))

if ok then
self.eventFilters[co] = param
else
table.remove(self.threads, i)
end

if coroutine.status(co) == "dead" then
self.threads[i].id = -1
self.threads[i].co = nil
table.remove(self.threads, i)
end
end
end

--[[
Runs the threads, passing event data to each one.
]]
function ThreadPool:waitForAll()
while #self.threads > 0 do
self:runOnce()
end
end

--[[
Runs the threads once and returns.
]]
function ThreadPool:runOnce()
for i = 1, #self.threads do
self:runThread(i)
end

self.eventData = {os.pullEventRaw()}
end

--[[
Adds a thread to this ThreadPool.
]]
function ThreadPool:add(thread)
thread.co = coroutine.create(function()
xpcall(thread.func, errorHandler)
end)
table.insert(self.threads, thread)
local id = #self.threads
self:runThread(id)
return id
end

SeeVM = { }
SeeVM.__index = SeeVM

@@ -261,7 +188,6 @@ function SeeVM.new(natives, seePath)
self.classes = { }
self.natives = natives
self.natives.error = error
self.natives.threadpool = ThreadPool.new()
self.classPaths = { }
self.base.Object = self:loadClassFromAny("see.base.Object")
self.rt = self:loadClassFromAny("see.rt.Class")
@@ -1,5 +1,4 @@
--@native coroutine
--@native threadpool

Thread.SUSPENDED = "suspended"
Thread.RUNNING = "running"
@@ -14,17 +13,19 @@ function Thread.yield(...)
return coroutine.yield(...)
end

function Thread.current()
return threadpool.current
end

--[[
Constructs a new Thread.
@param function:func The function to run in a new thread.
]]
function Thread:init(func)
self.func = func
self.id = threadpool:add(self)
self.co = coroutine.create(func)
end

--[[
Resumes a suspended coroutine.
]]
function Thread:resume()
coroutine.resume(self.co)
end

--[[
@@ -0,0 +1,17 @@
--@import see.io.InputStream

--@extends see.io.InputStream

--[[
Wraps around an array to read from.
@param Array:wrap The array to wrap around.
]]
function ArrayInputStream:init(wrap)
self.wrap = wrap
self.position = 1
end

function ArrayInputStream:read()
self.position = self.position + 1
return self.wrap[self.position - 1]
end
@@ -0,0 +1,17 @@
--@import see.io.OutputStream

--@extends see.io.OutputStream

--[[
Wraps around an array to write to.
@param Array:wrap The array to wrap around.
]]
function ArrayOutputStream:init(wrap)
self.wrap = wrap
self.position = 1
end

function ArrayOutputStream:write(b)
self.wrap[self.position] = b
self.position = self.position + 1
end
@@ -0,0 +1,57 @@
--@native bit

--@import see.io.InputStream

--@extends see.io.InputStream

--[[
An InputStream for doing more advanced operations on other InputStreams.
]]

--[[
Wraps arounds a see.io.InputStream.
@param see.io.InputStream:wrap The InputStream to wrap around.
]]
function DataInputStream:init(wrap)
self.wrap = wrap
end

function DataInputStream:read()
return self.wrap:read()
end

--[[
Reads an integer with the given amount of bytes.
@param number:bytes The number of bytes to encode the number with.
@return number The integer that was read.
]]
function DataInputStream:readInt(bytes)
local ret = 0
local b
for i = bytes, 1, -1 do
b = self:read()
ret = bit.bor(ret, bit.blshift(b, (i - 1) * 8))
end
return ret
end

--[[
Reads a string of a specific length.
@param number:len The length of the string to read.
@return see.base.String The string that was read.
]]
function DataInputStream:readString(len)
local ret = String.new()
for i = 1, len do
ret[i] = self:read()
end
return ret
end

function DataInputStream:flush()
self.wrap:flush()
end

function DataInputStream:close()
self.wrap:close()
end
@@ -0,0 +1,51 @@
--@native bit

--@import see.io.OutputStream

--@extends see.io.OutputStream

--[[
An OutputStream for doing more advanced operations on other OutputStreams.
]]

--[[
Wraps arounds a see.io.OutputStream.
@param see.io.OutputStream:wrap The OutputStream to wrap around.
]]
function DataOutputStream:init(wrap)
self.wrap = wrap
end

function DataOutputStream:write(b)
self.wrap:write(b)
end

--[[
Writes an integer with the given amount of bytes.
@param number:value The value to write.
@param number:bytes The number of bytes to encode the number with.
]]
function DataOutputStream:writeInt(value, bytes)
for i = bytes, 1, -1 do
self:write(bit.band(0xff, bit.brshift(value, (i - 1) * 8)))
end
end

--[[
Writes a string.
@param see.base.String:str The string to write.
]]
function DataOutputStream:writeString(str)
str = cast(str, String)
for i = 1, str:length() do
self:write(str[i])
end
end

function DataOutputStream:flush()
self.wrap:flush()
end

function DataOutputStream:close()
self.wrap:close()
end
@@ -1,21 +1,18 @@
--@import see.io.Path
--@import see.io.FileOutputStream
--@import see.io.FileInputStream
--@import see.io.Path
--@import see.io.Files
--@import see.io.DataOutputStream
--@import see.io.DataInputStream
--@import see.io.ArrayOutputStream
--@import see.io.ArrayInputStream

function Test.main()
local path = Path.new("/fostest")

if not Files.exists(path) then
System.print("File does not exist!")
return
end
local path = Path.new("/filetest")
local array = Array.new()

local fos = FileOutputStream.new(path)
fos:write(133)
fos:close()
local dos = DataOutputStream.new(ArrayOutputStream.new(array))
dos:writeString("Hello!\n")
local dis = DataInputStream.new(ArrayInputStream.new(array))

local fis = FileInputStream.new(path)
System.print(fis:read())
fis:close()
System.write(dis:readString(array:length()))
end

0 comments on commit f1224cf

Please sign in to comment.