Skip to content

Commit a61b117

Browse files
committedJun 18, 2011
Added threading and the beginings of Object dispatch
1 parent fe76c68 commit a61b117

8 files changed

+2560
-87
lines changed
 

‎Classes/DetailViewController.m

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#import "DetailViewController.h"
1010
#import "RootViewController.h"
11+
#import "PerformOMatic.h"
1112

1213
static void (*cb_viewDidLoad)(void*);
1314

@@ -29,6 +30,13 @@ void setLispEval(DetailViewController *vc, void (*cb)(void *vc, const char *))
2930
vc -> cb_lispEval = cb;
3031
}
3132

33+
void dispatchFunc(void (*fp)(void*)) {
34+
// dispatch_async_f(dispatch_get_main_queue(), NULL, fp);
35+
id runner = [[PerformOMatic alloc] init];
36+
[runner setFunc:fp];
37+
[runner run];
38+
}
39+
3240
@interface DetailViewController ()
3341
@property (nonatomic, retain) UIPopoverController *popoverController;
3442
- (void)configureView;

‎Classes/PerformOMatic.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// PerformOMatic.h
3+
// Lisp
4+
//
5+
// Created by David Pollak on 6/17/11.
6+
// Copyright 2011 lift Web Framework. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
12+
@interface PerformOMatic : NSObject {
13+
void (*whatToDo)(void *);
14+
15+
}
16+
17+
- (void)setFunc:(void *)func;
18+
- (void)run;
19+
- (void)reallyDoIt:(id)ignore;
20+
@end
21+
22+
extern void releaseMe(void *);

‎Classes/PerformOMatic.m

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// PerformOMatic.m
3+
// Lisp
4+
//
5+
// Created by David Pollak on 6/17/11.
6+
// Copyright 2011 lift Web Framework. All rights reserved.
7+
//
8+
9+
#import "PerformOMatic.h"
10+
11+
12+
@implementation PerformOMatic
13+
- (void)run {
14+
[self performSelectorOnMainThread:@selector(reallyDoIt:) withObject:self waitUntilDone:TRUE];
15+
}
16+
17+
- (void)reallyDoIt:(id)ignore {
18+
whatToDo(NULL);
19+
releaseMe(whatToDo);
20+
[self dealloc];
21+
}
22+
23+
- (void)setFunc:(void *)func {
24+
whatToDo = func;
25+
}
26+
27+
@end

‎DetailView.xib

+6-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
<object class="IBUIToolbar" id="410698538">
4646
<reference key="NSNextResponder" ref="647120888"/>
4747
<int key="NSvFlags">290</int>
48-
<string key="NSFrameSize">{768, 44}</string>
48+
<string key="NSFrameSize">{1024, 44}</string>
4949
<reference key="NSSuperview" ref="647120888"/>
5050
<bool key="IBUIOpaque">NO</bool>
5151
<bool key="IBUIClearsContextBeforeDrawing">NO</bool>
@@ -127,13 +127,16 @@
127127
</object>
128128
</object>
129129
</object>
130-
<string key="NSFrameSize">{768, 1004}</string>
130+
<string key="NSFrameSize">{1024, 748}</string>
131131
<reference key="NSSuperview"/>
132132
<reference key="IBUIBackgroundColor" ref="33107367"/>
133133
<bool key="IBUIClearsContextBeforeDrawing">NO</bool>
134134
<object class="IBUISimulatedStatusBarMetrics" key="IBUISimulatedStatusBarMetrics">
135135
<int key="IBUIStatusBarStyle">2</int>
136136
</object>
137+
<object class="IBUISimulatedOrientationMetrics" key="IBUISimulatedOrientationMetrics">
138+
<int key="interfaceOrientation">3</int>
139+
</object>
137140
<string key="targetRuntimeIdentifier">IBIPadFramework</string>
138141
</object>
139142
</object>
@@ -272,7 +275,7 @@
272275
<object class="NSAffineTransform">
273276
<bytes key="NSTransformStruct">P4AAAL+AAABDngAAxG/AAA</bytes>
274277
</object>
275-
<string>{{897, 532}, {768, 1024}}</string>
278+
<string>{{769, 660}, {1024, 768}}</string>
276279
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
277280
</object>
278281
</object>

‎Lisp.xcodeproj/dpp.mode1v3

+116-64
Large diffs are not rendered by default.

‎Lisp.xcodeproj/dpp.pbxuser

+2,310-14
Large diffs are not rendered by default.

‎Lisp.xcodeproj/project.pbxproj

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
2892E4100DC94CBA00A64D0F /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2892E40F0DC94CBA00A64D0F /* CoreGraphics.framework */; };
1818
28AD73600D9D9599002E5188 /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 28AD735F0D9D9599002E5188 /* MainWindow.xib */; };
1919
DE23F94C139EDDD10030BDEB /* libLisp.a in Frameworks */ = {isa = PBXBuildFile; fileRef = DE23F94B139EDDD10030BDEB /* libLisp.a */; };
20+
DEAB8A8913AC117200A67CC3 /* PerformOMatic.m in Sources */ = {isa = PBXBuildFile; fileRef = DEAB8A8813AC117200A67CC3 /* PerformOMatic.m */; };
2021
/* End PBXBuildFile section */
2122

2223
/* Begin PBXContainerItemProxy section */
@@ -46,6 +47,8 @@
4647
29B97316FDCFA39411CA2CEA /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
4748
8D1107310486CEB800E47090 /* Lisp-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Lisp-Info.plist"; plistStructureDefinitionIdentifier = "com.apple.xcode.plist.structure-definition.iphone.info-plist"; sourceTree = "<group>"; };
4849
DE23F94B139EDDD10030BDEB /* libLisp.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libLisp.a; path = dist/build/Lisp/libLisp.a; sourceTree = BUILT_PRODUCTS_DIR; };
50+
DEAB8A8713AC117200A67CC3 /* PerformOMatic.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PerformOMatic.h; sourceTree = "<group>"; };
51+
DEAB8A8813AC117200A67CC3 /* PerformOMatic.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PerformOMatic.m; sourceTree = "<group>"; };
4952
/* End PBXFileReference section */
5053

5154
/* Begin PBXFrameworksBuildPhase section */
@@ -72,6 +75,8 @@
7275
28042008108E984D000629CD /* RootViewController.m */,
7376
28042009108E984D000629CD /* DetailViewController.h */,
7477
2804200A108E984D000629CD /* DetailViewController.m */,
78+
DEAB8A8713AC117200A67CC3 /* PerformOMatic.h */,
79+
DEAB8A8813AC117200A67CC3 /* PerformOMatic.m */,
7580
);
7681
path = Classes;
7782
sourceTree = "<group>";
@@ -210,6 +215,7 @@
210215
1D3623260D0F684500981E51 /* LispAppDelegate.m in Sources */,
211216
2804200B108E984D000629CD /* RootViewController.m in Sources */,
212217
2804200C108E984D000629CD /* DetailViewController.m in Sources */,
218+
DEAB8A8913AC117200A67CC3 /* PerformOMatic.m in Sources */,
213219
);
214220
runOnlyForDeploymentPostprocessing = 0;
215221
};

‎Main.hs

+65-6
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import Monad
66
import Control.Monad.Error
77
import IO hiding (try)
88
import Data.IORef
9-
10-
9+
import System.Random
10+
import Control.Concurrent
1111
import Foreign.C.Types
1212
import Foreign.C.String
1313
import Foreign.Ptr
1414
import Network
1515
import System.IO
1616
import Data.IORef
17+
import System.IO.Unsafe
1718

1819
foreign import ccall safe "openWindow" openWindow
1920
:: IO CInt
@@ -28,6 +29,27 @@ foreign import ccall safe "setLispEval" setLispEval :: ViewController -> FunPtr
2829

2930
foreign import ccall safe "addToResult" addToResult :: ViewController -> CString -> IO ()
3031

32+
data ObjCId_struct
33+
type ObjCId = Ptr ObjCId_struct
34+
35+
data ObjCSEL_struct
36+
type ObjCSEL = Ptr ObjCSEL_struct
37+
38+
foreign import ccall safe "objc_msgSend" objc_msgSend :: ObjCId -> ObjCSEL -> IO ObjCId
39+
foreign import ccall safe "objc_msgSend" objc_msgSendInt :: ObjCId -> ObjCSEL -> Int -> IO ObjCId
40+
foreign import ccall safe "sel_registerName" sel_registerName :: CString -> IO ObjCSEL
41+
foreign import ccall safe "objc_lookUpClass" objc_lookUpClass :: CString -> IO ObjCId
42+
43+
44+
makeAString :: IO ObjCId
45+
makeAString = do
46+
strCls <- withCString "NSString" objc_lookUpClass
47+
allocName <- withCString "alloc" sel_registerName
48+
alloced <- objc_msgSend strCls allocName
49+
hPutStrLn stderr "Alloced it"
50+
initName <- withCString "init" sel_registerName
51+
objc_msgSend alloced initName
52+
3153
openLogger :: IO Handle
3254
openLogger = return stderr
3355

@@ -36,7 +58,42 @@ type ViewDidLoad = ViewController -> IO ()
3658
foreign import ccall safe "wrapper" mkViewDidLoad :: ViewDidLoad -> IO (FunPtr ViewDidLoad)
3759
foreign import ccall safe "setViewDidLoad" setViewDidLoad :: FunPtr ViewDidLoad -> IO ()
3860

39-
61+
foreign import ccall safe "dispatchFunc" dispatchFunc :: FunPtr StrVoid -> IO ()
62+
63+
type StrVoid = CString -> IO ()
64+
foreign import ccall safe "wrapper" mkStrCB :: StrVoid -> IO (FunPtr StrVoid)
65+
66+
foreign export ccall releaseMe :: FunPtr a -> IO ()
67+
releaseMe :: FunPtr a -> IO ()
68+
releaseMe ptr = freeHaskellFunPtr ptr
69+
70+
myThread :: IO ()
71+
myThread = do
72+
str <- makeAString
73+
log <- openLogger
74+
rSalt <- getStdRandom (randomR (1,1000000))
75+
threadDelay $ 1000000 + rSalt
76+
runOnMain $ appendStr $ "\nHello dude " ++ show rSalt
77+
myThread
78+
79+
runOnMain :: IO () -> IO ()
80+
runOnMain todo = do
81+
func <- funky
82+
dispatchFunc func
83+
where funky = mkStrCB $ \v -> do
84+
todo
85+
--func <- funky
86+
--freeHaskellFunPtr func
87+
88+
viewController :: IORef (Maybe ViewController)
89+
viewController = unsafePerformIO $ newIORef Nothing
90+
91+
appendStr :: String -> IO ()
92+
appendStr str = do
93+
vcm <- readIORef viewController
94+
case vcm of
95+
Just vc -> withCString str $ \cstr -> addToResult vc cstr
96+
_ -> return ()
4097
main :: IO ()
4198
main = do
4299
log <- openLogger
@@ -46,10 +103,12 @@ main = do
46103

47104
-- execute a line of List and call the callback with the result
48105
runALine <- wrapFuncInvoke $ \vc line -> do
106+
writeIORef viewController $ Just vc
49107
toEval <- peekCString line
50108
res <- evalString env toEval
51-
back <- withCString res $ \back -> addToResult vc back
52-
return ()
109+
appendStr res
110+
111+
forkOS myThread
53112

54113
-- the initial callback that sets up the rest
55114
vdl <- mkViewDidLoad $ \vc -> do
@@ -368,7 +427,7 @@ nullEnv = newIORef []
368427

369428
type IOThrowsError = ErrorT LispError IO
370429

371-
-- liftThrows :: ThrowsError a -> IOThrowsError a
430+
liftThrows :: ThrowsError a -> IOThrowsError a
372431
liftThrows (Left err) = throwError err
373432
liftThrows (Right val) = return val
374433

0 commit comments

Comments
 (0)
Please sign in to comment.