[Assorted-commits] SF.net SVN: assorted: [843] sandbox/trunk/src
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-06-02 00:26:06
|
Revision: 843 http://assorted.svn.sourceforge.net/assorted/?rev=843&view=rev Author: yangzhang Date: 2008-06-01 17:26:13 -0700 (Sun, 01 Jun 2008) Log Message: ----------- added stuff to sandbox Added Paths: ----------- sandbox/trunk/src/cc/stringsplitting.cc sandbox/trunk/src/hs/BlockingIo.hs sandbox/trunk/src/hs/Introspection.hs sandbox/trunk/src/hs/Signals.hs sandbox/trunk/src/hs/ThreadPool.hs sandbox/trunk/src/py/af/sockcancel.py sandbox/trunk/src/scala/SetsIntersectionBug.scala Added: sandbox/trunk/src/cc/stringsplitting.cc =================================================================== --- sandbox/trunk/src/cc/stringsplitting.cc (rev 0) +++ sandbox/trunk/src/cc/stringsplitting.cc 2008-06-02 00:26:13 UTC (rev 843) @@ -0,0 +1,26 @@ +// Simple string splitter. How does this work? istream_iterator<T> reads +// objects of type T in from the given istream and yields them as an iterator. + +#include <string> +#include <iostream> +#include <sstream> +#include <iterator> +#include <vector> +using namespace std; + +std::vector<std::string> split(const std::string& s) +{ + std::istringstream is(s); + return std::vector<string>(std::istream_iterator<std::string>(is), + std::istream_iterator<std::string>()); +} + +int main() { + vector<string> words = split(string("hello world")); + for (vector<string>::const_iterator word = words.begin(); + word != words.end(); + word++) { + cout << *word << endl; + } + return 0; +} Added: sandbox/trunk/src/hs/BlockingIo.hs =================================================================== --- sandbox/trunk/src/hs/BlockingIo.hs (rev 0) +++ sandbox/trunk/src/hs/BlockingIo.hs 2008-06-02 00:26:13 UTC (rev 843) @@ -0,0 +1,28 @@ +module Main where + +-- Simple demo of the interaction between blocking IO and lightweight +-- threads. Haskell is smart and creates real threads on demand. + +import Control.Concurrent +import Network.FTP.Client + +main = do +-- installHandler + tid <- myThreadId + forkIO $ watchdog tid + beta + +watchdog tid = do + threadDelay 1000000 + putStrLn "killing" + killThread tid + +beta = do + enableFTPDebugging + h <- easyConnectFTP "1.1.1.1" -- "ftp.kernel.org" + loginAnon h + cwd h "/pub/linux/kernel/Historic" + nlst h Nothing >>= putStrLn . unlines + getbinary h "linux-0.01.tar.gz.sign" >>= putStrLn . fst + dir h Nothing >>= putStrLn . unlines + quit h Added: sandbox/trunk/src/hs/Introspection.hs =================================================================== --- sandbox/trunk/src/hs/Introspection.hs (rev 0) +++ sandbox/trunk/src/hs/Introspection.hs 2008-06-02 00:26:13 UTC (rev 843) @@ -0,0 +1,9 @@ +module Main where + +import Data.Generics.Text + +data T a = B (T a) (T a) | L a + +-- http://www.defmacro.org/ramblings/haskell-web.html + +main = print (gshow (L 3)) \ No newline at end of file Added: sandbox/trunk/src/hs/Signals.hs =================================================================== --- sandbox/trunk/src/hs/Signals.hs (rev 0) +++ sandbox/trunk/src/hs/Signals.hs 2008-06-02 00:26:13 UTC (rev 843) @@ -0,0 +1,29 @@ +module Main where + +-- summary: signals are handled in separate threads and don't +-- interrupt syscalls + +import Control.Concurrent +import System.IO +import System.Process +import System.Posix.Signals + +hGetContents' h = do + c <- hGetContents h + length c `seq` return c + +main = do + let handler = do + tid <- myThreadId + putStrLn $ "handling in " ++ show tid + tid <- myThreadId + putStrLn $ "thread " ++ show tid + installHandler sigUSR1 (Catch handler) Nothing + putStrLn "spawning" + (inp,out,err,pid) <- runInteractiveCommand "echo hello; sleep 5; echo world; exit 1" + putStrLn "reading" + res <- hGetContents' out + putStrLn "waiting" + status <- waitForProcess pid + putStrLn $ "exited with " ++ show status + putStrLn $ "read " ++ res Added: sandbox/trunk/src/hs/ThreadPool.hs =================================================================== --- sandbox/trunk/src/hs/ThreadPool.hs (rev 0) +++ sandbox/trunk/src/hs/ThreadPool.hs 2008-06-02 00:26:13 UTC (rev 843) @@ -0,0 +1,42 @@ +module Main where + +import Control.Monad +import Control.Concurrent +import Control.Exception as E +import Control.Concurrent.STM + +type Work = IO () + +type SendWork = Work -> STM () + +spawnWorkers :: Int -> IO (SendWork,IO ()) +spawnWorkers i | i <= 0 = error "Need positive number of workers" + | otherwise = do + + workChan <- atomically newTChan + runCount <- atomically (newTVar i) + + let stop = atomically (writeTVar runCount . pred =<< readTVar runCount) + die e = do id <- myThreadId + print ("Thread "++show id++" died with exception "++show e) + stop + work = do mJob <- atomically (readTChan workChan) + case mJob of Nothing -> stop + Just job -> E.catch job die >> work + replicateM_ i (forkIO work) + + let stopCommand = do atomically (replicateM_ i (writeTChan workChan Nothing)) + atomically (do running <- readTVar runCount + when (running>0) retry) + + return (writeTChan workChan . Just,stopCommand) + +printJob i = do threadDelay (1000 * i) + id <- myThreadId + print ("printJob took "++show i++" ms in thread "++show id) + +demo = do + (submit,stop) <- spawnWorkers 4 + mapM_ (atomically . submit . printJob) (take 40 (cycle [100,200,300,400])) + atomically $ submit (error "Boom") + stop Added: sandbox/trunk/src/py/af/sockcancel.py =================================================================== --- sandbox/trunk/src/py/af/sockcancel.py (rev 0) +++ sandbox/trunk/src/py/af/sockcancel.py 2008-06-02 00:26:13 UTC (rev 843) @@ -0,0 +1,21 @@ +#!/usr/bin/env python +# vim:et:sw=2:ts=2 + +""" +Shows that socket.read_some()'s cancel-handling is broken. +""" + +from __future__ import generators + +import af +import afx.all as afx + +@af.task +def main(argv): + s = yield af.socket.connect('localhost',9876) + u = afx.socket_unpickler(s) + while True: + res = yield u.read() | af.timeout(1) + print res + +afx.run_main() Property changes on: sandbox/trunk/src/py/af/sockcancel.py ___________________________________________________________________ Name: svn:executable + * Added: sandbox/trunk/src/scala/SetsIntersectionBug.scala =================================================================== --- sandbox/trunk/src/scala/SetsIntersectionBug.scala (rev 0) +++ sandbox/trunk/src/scala/SetsIntersectionBug.scala 2008-06-02 00:26:13 UTC (rev 843) @@ -0,0 +1,8 @@ +import scala.collection.mutable._ +object SetsIntersectionBug extends Application { + val m1,m2 = new HashMap[Int,Int] + // This yields a build error, which seems like a compiler bug, since this + // works in the REPL. + val s = m1.keySet ** m2.keySet + println(s) +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |