summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_threadsignals.py
blob: fa26583472b1b429f4f8df4bf89a483d0d6a5f70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""PyUnit testing that threads honor our signal semantics"""

import unittest
import signal
import os
import sys
from test.support import run_unittest, import_module
thread = import_module('_thread')
import time

if sys.platform[:3] in ('win', 'os2') or sys.platform=='riscos':
    raise unittest.SkipTest("Can't test signal on %s" % sys.platform)

process_pid = os.getpid()
signalled_all=thread.allocate_lock()


def registerSignals(for_usr1, for_usr2, for_alrm):
    usr1 = signal.signal(signal.SIGUSR1, for_usr1)
    usr2 = signal.signal(signal.SIGUSR2, for_usr2)
    alrm = signal.signal(signal.SIGALRM, for_alrm)
    return usr1, usr2, alrm


# The signal handler. Just note that the signal occurred and
# from who.
def handle_signals(sig,frame):
    signal_blackboard[sig]['tripped'] += 1
    signal_blackboard[sig]['tripped_by'] = thread.get_ident()

# a function that will be spawned as a separate thread.
def send_signals():
    print("send_signals: enter (thread %s)" % thread.get_ident(), file=sys.stderr)
    print("send_signals: raise SIGUSR1", file=sys.stderr)
    os.kill(process_pid, signal.SIGUSR1)
    print("send_signals: raise SIGUSR2", file=sys.stderr)
    os.kill(process_pid, signal.SIGUSR2)
    print("send_signals: release signalled_all", file=sys.stderr)
    signalled_all.release()
    print("send_signals: exit (thread %s)" % thread.get_ident(), file=sys.stderr)

class ThreadSignals(unittest.TestCase):

    def test_signals(self):
        # Test signal handling semantics of threads.
        # We spawn a thread, have the thread send two signals, and
        # wait for it to finish. Check that we got both signals
        # and that they were run by the main thread.
        print("test_signals: acquire lock (thread %s)" % thread.get_ident(), file=sys.stderr)
        signalled_all.acquire()
        self.spawnSignallingThread()
        print("test_signals: wait lock (thread %s)" % thread.get_ident(), file=sys.stderr)
        signalled_all.acquire()
        print("test_signals: lock acquired", file=sys.stderr)
        # the signals that we asked the kernel to send
        # will come back, but we don't know when.
        # (it might even be after the thread exits
        # and might be out of order.)  If we haven't seen
        # the signals yet, send yet another signal and
        # wait for it return.
        if signal_blackboard[signal.SIGUSR1]['tripped'] == 0 \
           or signal_blackboard[signal.SIGUSR2]['tripped'] == 0:
            signal.alarm(1)
            signal.pause()
            signal.alarm(0)

        self.assertEqual( signal_blackboard[signal.SIGUSR1]['tripped'], 1)
        self.assertEqual( signal_blackboard[signal.SIGUSR1]['tripped_by'],
                           thread.get_ident())
        self.assertEqual( signal_blackboard[signal.SIGUSR2]['tripped'], 1)
        self.assertEqual( signal_blackboard[signal.SIGUSR2]['tripped_by'],
                           thread.get_ident())
        signalled_all.release()

    def spawnSignallingThread(self):
        thread.start_new_thread(send_signals, ())

    def alarm_interrupt(self, sig, frame):
        raise KeyboardInterrupt

    def test_lock_acquire_interruption(self):
        # Mimic receiving a SIGINT (KeyboardInterrupt) with SIGALRM while stuck
        # in a deadlock.
        # XXX this test can fail when the legacy (non-semaphore) implementation
        # of locks is used in thread_pthread.h, see issue #11223.
        oldalrm = signal.signal(signal.SIGALRM, self.alarm_interrupt)
        try:
            lock = thread.allocate_lock()
            lock.acquire()
            signal.alarm(1)
            t1 = time.time()
            self.assertRaises(KeyboardInterrupt, lock.acquire, timeout=5)
            dt = time.time() - t1
            # Checking that KeyboardInterrupt was raised is not sufficient.
            # We want to assert that lock.acquire() was interrupted because
            # of the signal, not that the signal handler was called immediately
            # after timeout return of lock.acquire() (which can fool assertRaises).
            self.assertLess(dt, 3.0)
        finally:
            signal.signal(signal.SIGALRM, oldalrm)

    def test_rlock_acquire_interruption(self):
        # Mimic receiving a SIGINT (KeyboardInterrupt) with SIGALRM while stuck
        # in a deadlock.
        # XXX this test can fail when the legacy (non-semaphore) implementation
        # of locks is used in thread_pthread.h, see issue #11223.
        oldalrm = signal.signal(signal.SIGALRM, self.alarm_interrupt)
        try:
            rlock = thread.RLock()
            # For reentrant locks, the initial acquisition must be in another
            # thread.
            def other_thread():
                rlock.acquire()
            thread.start_new_thread(other_thread, ())
            # Wait until we can't acquire it without blocking...
            while rlock.acquire(blocking=False):
                rlock.release()
                time.sleep(0.01)
            signal.alarm(1)
            t1 = time.time()
            self.assertRaises(KeyboardInterrupt, rlock.acquire, timeout=5)
            dt = time.time() - t1
            # See rationale above in test_lock_acquire_interruption
            self.assertLess(dt, 3.0)
        finally:
            signal.signal(signal.SIGALRM, oldalrm)

    def acquire_retries_on_intr(self, lock):
        self.sig_recvd = False
        def my_handler(signal, frame):
            self.sig_recvd = True
        old_handler = signal.signal(signal.SIGUSR1, my_handler)
        try:
            def other_thread():
                # Acquire the lock in a non-main thread, so this test works for
                # RLocks.
                lock.acquire()
                # Wait until the main thread is blocked in the lock acquire, and
                # then wake it up with this.
                time.sleep(0.5)
                os.kill(process_pid, signal.SIGUSR1)
                # Let the main thread take the interrupt, handle it, and retry
                # the lock acquisition.  Then we'll let it run.
                time.sleep(0.5)
                lock.release()
            thread.start_new_thread(other_thread, ())
            # Wait until we can't acquire it without blocking...
            while lock.acquire(blocking=False):
                lock.release()
                time.sleep(0.01)
            result = lock.acquire()  # Block while we receive a signal.
            self.assertTrue(self.sig_recvd)
            self.assertTrue(result)
        finally:
            signal.signal(signal.SIGUSR1, old_handler)

    def test_lock_acquire_retries_on_intr(self):
        self.acquire_retries_on_intr(thread.allocate_lock())

    def test_rlock_acquire_retries_on_intr(self):
        self.acquire_retries_on_intr(thread.RLock())

    def test_interrupted_timed_acquire(self):
        # Test to make sure we recompute lock acquisition timeouts when we
        # receive a signal.  Check this by repeatedly interrupting a lock
        # acquire in the main thread, and make sure that the lock acquire times
        # out after the right amount of time.
        # NOTE: this test only behaves as expected if C signals get delivered
        # to the main thread.  Otherwise lock.acquire() itself doesn't get
        # interrupted and the test trivially succeeds.
        self.start = None
        self.end = None
        self.sigs_recvd = 0
        done = thread.allocate_lock()
        done.acquire()
        lock = thread.allocate_lock()
        lock.acquire()
        def my_handler(signum, frame):
            self.sigs_recvd += 1
        old_handler = signal.signal(signal.SIGUSR1, my_handler)
        try:
            def timed_acquire():
                self.start = time.time()
                lock.acquire(timeout=0.5)
                self.end = time.time()
            def send_signals():
                for _ in range(40):
                    time.sleep(0.02)
                    os.kill(process_pid, signal.SIGUSR1)
                done.release()

            # Send the signals from the non-main thread, since the main thread
            # is the only one that can process signals.
            thread.start_new_thread(send_signals, ())
            timed_acquire()
            # Wait for thread to finish
            done.acquire()
            # This allows for some timing and scheduling imprecision
            self.assertLess(self.end - self.start, 2.0)
            self.assertGreater(self.end - self.start, 0.3)
            # If the signal is received several times before PyErr_CheckSignals()
            # is called, the handler will get called less than 40 times. Just
            # check it's been called at least once.
            self.assertGreater(self.sigs_recvd, 0)
        finally:
            signal.signal(signal.SIGUSR1, old_handler)


def test_main():
    global signal_blackboard

    signal_blackboard = { signal.SIGUSR1 : {'tripped': 0, 'tripped_by': 0 },
                          signal.SIGUSR2 : {'tripped': 0, 'tripped_by': 0 },
                          signal.SIGALRM : {'tripped': 0, 'tripped_by': 0 } }

    oldsigs = registerSignals(handle_signals, handle_signals, handle_signals)
    try:
        run_unittest(ThreadSignals)
    finally:
        registerSignals(*oldsigs)

if __name__ == '__main__':
    test_main()
t;uscxml/util/UUID.h" //#include "uscxml/server/HTTPServer.h" //#include "uscxml/plugins/invoker/dirmon/DirMonInvoker.h" #include "uscxml/plugins/datamodel/promela/PromelaDataModel.h" #ifdef FEATS_ON_CMD #ifdef WITH_DM_ECMA_V8 #include "uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.h" #endif #ifdef WITH_DM_ECMA_JSC #include "uscxml/plugins/datamodel/ecmascript/JavaScriptCore/JSCDataModel.h" #endif #ifdef WITH_DM_LUA #include "uscxml/plugins/datamodel/lua/LuaDataModel.h" #endif #endif #include "uscxml/interpreter/InterpreterImpl.h" #include "uscxml/interpreter/BasicEventQueue.h" #include "uscxml/interpreter/BasicDelayedEventQueue.h" #ifdef BUILD_PROFILING # include "uscxml/plugins/DataModel.h" # endif #define USER_DATA(ctx) ((StateMachine*)(((uscxml_ctx*)ctx)->user_data)) using namespace uscxml; namespace XERCESC_NS { class DOMDocument; class DOMNode; } class StateMachine : public DataModelCallbacks, public IOProcessorCallbacks, public DelayedEventQueueCallbacks, public InvokerCallbacks { public: StateMachine(const uscxml_machine* machine) : machine(machine), parentMachine(NULL), topMostMachine(NULL), invocation(NULL) { allMachines[sessionId] = this; topMostMachine = this; currentMachine = allMachines.begin(); try { init(); } catch (ErrorEvent e) { LOGD(USCXML_FATAL) << e; } } StateMachine(StateMachine* parent, const uscxml_machine* machine, const uscxml_elem_invoke* invoke) : machine(machine), invocation(invoke) { parentMachine = parent; topMostMachine = parent->topMostMachine; init(); } ActionLanguage* getActionLanguage() { return NULL; } std::set<InterpreterMonitor*> getMonitors() { return std::set<InterpreterMonitor*>(); } std::string getBaseURL() { return ""; } virtual Logger getLogger() { return Logger::getDefault(); } const std::string& getName() { return name; } const std::string& getSessionId() { return sessionId; } const std::map<std::string, IOProcessor>& getIOProcessors() { return ioProcs; } void enqueueInternal(const Event& event) { iq.push_back(event); } void enqueueExternal(const Event& event) { eq.push_back(event); } void enqueueAtInvoker(const std::string& invokeId, const Event& event) { if (invokers.find(invokeId) != invokers.end()) invokers[invokeId].eventFromSCXML(event); } void enqueueAtParent(const Event& event) { if (parentMachine != NULL) parentMachine->enqueueExternal(event); } bool isInState(const std::string& stateId) { for (size_t i = 0; i < ctx.machine->nr_states; i++) { if (ctx.machine->states[i].name && strcmp(ctx.machine->states[i].name, stateId.c_str()) == 0 && BIT_HAS(i, ctx.config)) { return true; } } return false; } XERCESC_NS::DOMDocument* getDocument() const { return NULL; } const std::map<std::string, Invoker>& getInvokers() { return invokers; } void init() { sessionId = uscxml::UUID::getUUID(); isFinalized = false; // clear and initialize machine context memset(&ctx, 0, sizeof(uscxml_ctx)); ctx.machine = machine; ctx.user_data = (void*)this; // register callbacks with scxml context ctx.is_matched = &isMatched; ctx.is_true = &isTrue; ctx.raise_done_event = &raiseDoneEvent; ctx.invoke = &invoke; ctx.exec_content_send = &execContentSend; ctx.exec_content_raise = &execContentRaise; ctx.exec_content_cancel = &execContentCancel; ctx.exec_content_log = &execContentLog; ctx.exec_content_assign = &execContentAssign; ctx.exec_content_foreach_init = &execContentForeachInit; ctx.exec_content_foreach_next = &execContentForeachNext; ctx.exec_content_foreach_done = &execContentForeachDone; ctx.dequeue_external = &dequeueExternal; ctx.dequeue_internal = &dequeueInternal; ctx.exec_content_init = &execContentInit; ctx.exec_content_script = &execContentScript; name = machine->name; // register IO Procs std::map<std::string, IOProcessorImpl*> allIOProcs = Factory::getInstance()->getIOProcessors(); for (auto ioProcImpl : allIOProcs) { ioProcs[ioProcImpl.first] = Factory::getInstance()->createIOProcessor(ioProcImpl.first, this); std::list<std::string> names = ioProcImpl.second->getNames(); for (auto name : names) { ioProcs[name] = ioProcs[ioProcImpl.first]; } } delayQueue = DelayedEventQueue(std::shared_ptr<DelayedEventQueueImpl>(new BasicDelayedEventQueue(this))); dataModel = Factory::getInstance()->createDataModel(machine->datamodel, this); if (invocation != NULL) { /// test 226/240 - initialize from invoke request if (invocation->params != NULL) { const uscxml_elem_param* param = invocation->params; while(USCXML_ELEM_PARAM_IS_SET(param)) { std::string identifier; if (param->name != NULL) { identifier = param->name; } else if (param->location != NULL) { identifier = param->location; } invokeData[identifier] = parentMachine->dataModel.evalAsData(param->expr); param++; } } if (invocation->namelist != NULL) { const char* cPtr = invocation->namelist; const char* aPtr = invocation->namelist; while(cPtr) { while (isspace(*cPtr)) cPtr++; aPtr = cPtr; while(*cPtr && !isspace(*cPtr)) cPtr++; if (aPtr == cPtr) break; std::string identifier = std::string(aPtr, cPtr - aPtr); invokeData[identifier] = parentMachine->dataModel.evalAsData(identifier); } } } } virtual ~StateMachine() { if (parentMachine != NULL) { topMostMachine->allMachines.erase(topMostMachine->invocationIds[invocation]); } // finalize(); delayQueue.cancelAllDelayed(); while(eq.size() > 0) { eq.pop_front(); } eq.clear(); while(iq.size() > 0) { iq.pop_front(); } iq.clear(); } bool hasPendingWork() { return (iq.size() > 0 || eq.size() > 0 || ctx.flags & USCXML_CTX_SPONTANEOUS || ctx.flags == USCXML_CTX_PRISTINE || memcmp(ctx.config, ctx.invocations, sizeof(ctx.config)) != 0); } bool isDone() { return ctx.flags & USCXML_CTX_FINISHED; } void finalize() { if (isFinalized) return; delayQueue.cancelAllDelayed(); if (parentMachine != NULL) { std::lock_guard<std::mutex> lock(mutex); Event done; done.invokeid = invokeId; done.name = "done.invoke." + invokeId; parentMachine->eq.push_back(done); } isFinalized = true; } void reset() { delayQueue.cancelAllDelayed(); while(eq.size() > 0) { eq.pop_front(); } while(iq.size() > 0) { iq.pop_front(); } iq.clear(); eq.clear(); init(); } int step() { // advance current machine if there are multiple currentMachine++; if (currentMachine == allMachines.end()) currentMachine = allMachines.begin(); StateMachine* toRun = currentMachine->second; if (!toRun->hasPendingWork()) { return USCXML_ERR_IDLE; } // test 187 if (toRun->isDone()) { toRun->finalize(); return USCXML_ERR_IDLE; } state = uscxml_step(&toRun->ctx); return state; } // callbacks for scxml context static int isMatched(const uscxml_ctx* ctx, const uscxml_transition* t, const void* e) { Event* event = (Event*)e; return (nameMatch(t->event, event->name.c_str())); } static int isTrue(const uscxml_ctx* ctx, const char* expr) { try { return USER_DATA(ctx)->dataModel.evalAsBool(expr); } catch (Event e) { execContentRaise(ctx, e.name.c_str()); } return false; } static int invoke(const uscxml_ctx* ctx, const uscxml_state* s, const uscxml_elem_invoke* invocation, unsigned char uninvoke) { std::map<std::string, StateMachine*> &allMachines = USER_DATA(ctx)->topMostMachine->allMachines; StateMachine* topMachine = USER_DATA(ctx)->topMostMachine; if (uninvoke) { if (invocation->machine != NULL) { if (topMachine->invocationIds.find(invocation) != topMachine->invocationIds.end() && allMachines.find(topMachine->invocationIds[invocation]) != allMachines.end()) { delete allMachines[topMachine->invocationIds[invocation]]; topMachine->allMachines.erase(topMachine->invocationIds[invocation]); topMachine->invocationIds.erase(invocation); } } else { // TODO: Uninvoke other types of invokers return USCXML_ERR_UNSUPPORTED; } } else { // invocations if (invocation->machine != NULL) { // invoke a nested SCXML machine StateMachine* invokedMachine = NULL; try { invokedMachine = new StateMachine(USER_DATA(ctx), invocation->machine, invocation); } catch (Event e) { delete invokedMachine; return USCXML_ERR_EXEC_CONTENT; } if (invocation->id != NULL) { invokedMachine->invokeId = invocation->id; } else if (invocation->idlocation != NULL) { // test224 invokedMachine->invokeId = (invocation->sourcename != NULL ? std::string(invocation->sourcename) + "." : "") + uscxml::UUID::getUUID(); USER_DATA(ctx)->dataModel.assign(invocation->idlocation, Data(invokedMachine->invokeId, Data::VERBATIM)); } else { invokedMachine->invokeId = uscxml::UUID::getUUID(); } allMachines[invokedMachine->invokeId] = invokedMachine; topMachine->invocationIds[invocation] = invokedMachine->invokeId; } else if (Factory::getInstance()->hasInvoker(invocation->type)) { Event invokeEvent; // see BasicContentExecutor::384ff // TODO: Establish the invokeEvent if (invocation->params != NULL) { } Invoker inv = Factory::getInstance()->createInvoker(invocation->type, USER_DATA(ctx)); inv.invoke("", invokeEvent); USER_DATA(ctx)->_invocations[invocation] = inv; } else { return USCXML_ERR_UNSUPPORTED; } } return USCXML_ERR_OK; } static int raiseDoneEvent(const uscxml_ctx* ctx, const uscxml_state* state, const uscxml_elem_donedata* donedata) { Event e; e.name = std::string("done.state.") + state->name; if (donedata) { if (donedata->content != NULL) { if (isNumeric(donedata->content, 10)) { // test 529 e.data = Data(strTo<double>(donedata->content), Data::INTERPRETED); } else { e.data = Data(donedata->content, Data::VERBATIM); } } else if (donedata->contentexpr != NULL) { try { e.data = USER_DATA(ctx)->dataModel.getAsData(donedata->contentexpr); } catch (Event e) { execContentRaise(ctx, e.name.c_str()); } } else { try { const uscxml_elem_param* param = donedata->params; while (param && USCXML_ELEM_PARAM_IS_SET(param)) { Data paramValue; if (param->expr != NULL) { paramValue = USER_DATA(ctx)->dataModel.evalAsData(param->expr); } else if(param->location) { paramValue = USER_DATA(ctx)->dataModel.evalAsData(param->location); } e.params.insert(std::make_pair(param->name, paramValue)); param++; } } catch (Event e) { execContentRaise(ctx, e.name.c_str()); } } } #ifdef USCXML_VERBOSE printf("Raising Done Event: %s\n", e.name.c_str()); #endif USER_DATA(ctx)->iq.push_back(e); return USCXML_ERR_OK; } static int execContentSend(const uscxml_ctx* ctx, const uscxml_elem_send* send) { Event e; std::string sendid; if (send->id != NULL) { sendid = send->id; } else { sendid = uscxml::UUID::getUUID(); if (send->idlocation != NULL) { USER_DATA(ctx)->dataModel.assign(send->idlocation, Data(sendid, Data::VERBATIM)); } else { e.hideSendId = true; } } e.sendid = sendid; std::string target; if (send->target != NULL) { target = send->target; } else if (send->targetexpr != NULL) { target = USER_DATA(ctx)->dataModel.evalAsData(send->targetexpr).atom; } else { target = "#_external"; } if (target.size() > 0 && (target[0] != '#' || target[1] != '_')) { std::cerr << "Target '" << target << "' is not supported yet" << std::endl; e.name = "error.execution"; execContentRaise(ctx, e); return USCXML_ERR_INVALID_TARGET; } e.origintype = "http://www.w3.org/TR/scxml/#SCXMLEventProcessor"; std::string type; try { if (send->type != NULL) { type = send->type; } else if (send->typeexpr != NULL) { type = USER_DATA(ctx)->dataModel.evalAsData(send->typeexpr).atom; } else { type = "http://www.w3.org/TR/scxml/#SCXMLEventProcessor"; } } catch (Event exc) { e.name = "error.execution"; execContentRaise(ctx, e); return USCXML_ERR_EXEC_CONTENT; } // only one somewhat supported if (type != "http://www.w3.org/TR/scxml/#SCXMLEventProcessor") { e.name = "error.execution"; execContentRaise(ctx, e); return USCXML_ERR_INVALID_TARGET; } e.origintype = type; e.origin = "#_scxml_" + USER_DATA(ctx)->sessionId; e.invokeid = USER_DATA(ctx)->invokeId; if (send->eventexpr != NULL) { e.name = USER_DATA(ctx)->dataModel.evalAsData(send->eventexpr).atom; } else { e.name = send->event; } try { const uscxml_elem_param* param = send->params; while (param && USCXML_ELEM_PARAM_IS_SET(param)) { Data paramValue; if (param->expr != NULL) { paramValue = USER_DATA(ctx)->dataModel.evalAsData(param->expr); } else if(param->location) { paramValue = USER_DATA(ctx)->dataModel.evalAsData(param->location); } e.params.insert(std::make_pair(param->name, paramValue)); param++; } } catch (Event e) { execContentRaise(ctx, e.name.c_str()); return USCXML_ERR_EXEC_CONTENT; } try { if (send->namelist != NULL) { const char* bPtr = &send->namelist[0]; const char* ePtr = bPtr; while(*ePtr != '\0') { ePtr++; if (*ePtr == ' ' || *ePtr == '\0') { std::string key(bPtr, ePtr - bPtr); e.params.insert(std::make_pair(key, USER_DATA(ctx)->dataModel.evalAsData(key))); if (*ePtr == '\0') break; bPtr = ++ePtr; } } } } catch (Event e) { execContentRaise(ctx, e.name.c_str()); return USCXML_ERR_EXEC_CONTENT; } if (send->content != NULL) { try { // will it parse as json? Data d = USER_DATA(ctx)->dataModel.getAsData(send->content); if (!d.empty()) { e.data = d; } } catch (Event err) { e.data = Data(spaceNormalize(send->content), Data::VERBATIM); } } size_t delayMs = 0; std::string delay; if (send->delayexpr != NULL) { delay = USER_DATA(ctx)->dataModel.evalAsData(send->delayexpr).atom; } else if (send->delay != NULL) { delay = send->delay; } if (delay.size() > 0) { boost::trim(delay); NumAttr delayAttr(delay); if (iequals(delayAttr.unit, "ms")) { delayMs = strTo<uint32_t>(delayAttr.value); } else if (iequals(delayAttr.unit, "s")) { delayMs = strTo<double>(delayAttr.value) * 1000; } else if (delayAttr.unit.length() == 0) { // unit less delay is interpreted as milliseconds delayMs = strTo<uint32_t>(delayAttr.value); } else { std::cerr << "Cannot make sense of delay value " << delay << ": does not end in 's' or 'ms'"; } } if (USER_DATA(ctx)->invokeId.size() > 0) { e.invokeid = USER_DATA(ctx)->invokeId; } USER_DATA(ctx)->sendUUIDs[e.uuid] = std::make_tuple(e.sendid, target, type); if (delayMs > 0) { USER_DATA(ctx)->delayQueue.enqueueDelayed(e, delayMs, e.uuid); } else { USER_DATA(ctx)->eventReady(e, e.uuid); } return USCXML_ERR_OK; } static int execContentRaise(const uscxml_ctx* ctx, Event& e) { if (boost::starts_with(e.name, "error.")) { e.eventType = Event::PLATFORM; } else { e.eventType = Event::INTERNAL; } USER_DATA(ctx)->iq.push_back(e); return USCXML_ERR_OK; } static int execContentRaise(const uscxml_ctx* ctx, const char* event) { Event e; e.name = event; return execContentRaise(ctx, e); } static int execContentCancel(const uscxml_ctx* ctx, const char* sendid, const char* sendidexpr) { std::string eventId; if (sendid != NULL) { eventId = sendid; } else if (sendidexpr != NULL) { eventId = USER_DATA(ctx)->dataModel.evalAsData(sendidexpr).atom; } if (eventId.length() > 0) { // find all events with given id for (auto evIter = USER_DATA(ctx)->sendUUIDs.begin(); evIter != USER_DATA(ctx)->sendUUIDs.end(); evIter++) { std::string sendid = std::get<0>(evIter->second); if (eventId == sendid) { USER_DATA(ctx)->delayQueue.cancelDelayed(evIter->first); } } } else { execContentRaise(ctx, "error.execution"); return USCXML_ERR_EXEC_CONTENT; } return USCXML_ERR_OK; } static int execContentLog(const uscxml_ctx* ctx, const char* label, const char* expr) { try { if (label != NULL) { printf("%s%s", label, (expr != NULL ? ": " : "")); } if (expr != NULL) { std::string msg = USER_DATA(ctx)->dataModel.evalAsData(expr).atom; printf("%s", msg.c_str()); } if (label != NULL || expr != NULL) { printf("\n"); } } catch (Event e) { execContentRaise(ctx, e.name.c_str()); return USCXML_ERR_EXEC_CONTENT; } return USCXML_ERR_OK; } static int execContentAssign(const uscxml_ctx* ctx, const uscxml_elem_assign* assign) { std::string key = assign->location; if (key == "_sessionid" || key == "_name" || key == "_ioprocessors" || key == "_invokers" || key == "_event") { execContentRaise(ctx, "error.execution"); return USCXML_ERR_EXEC_CONTENT; } try { // Data d = USER_DATA(ctx)->dataModel.getStringAsData(expr); if (assign->expr != NULL) { // USER_DATA(ctx)->dataModel.assign(key, // USER_DATA(ctx)->dataModel.evalAsData(assign->expr)); USER_DATA(ctx)->dataModel.assign(key, Data(assign->expr, Data::INTERPRETED)); } else if (assign->content != NULL) { Data d = Data(assign->content, Data::INTERPRETED); USER_DATA(ctx)->dataModel.assign(key, d); } } catch (Event e) { execContentRaise(ctx, e.name.c_str()); return USCXML_ERR_EXEC_CONTENT; } return USCXML_ERR_OK; } static int execContentForeachInit(const uscxml_ctx* ctx, const uscxml_elem_foreach* foreach) { try { scxml_foreach_info* feInfo = (scxml_foreach_info*)malloc(sizeof(scxml_foreach_info)); USER_DATA(ctx)->foreachInfo[foreach] = feInfo; feInfo->iterations = USER_DATA(ctx)->dataModel.getLength(foreach->array); feInfo->currIteration = 0; } catch (Event e) { execContentRaise(ctx, e.name.c_str()); return USCXML_ERR_EXEC_CONTENT; } return USCXML_ERR_OK; } static int execContentForeachNext(const uscxml_ctx* ctx, const uscxml_elem_foreach* foreach) { try { scxml_foreach_info* feInfo = USER_DATA(ctx)->foreachInfo[foreach]; if (feInfo->currIteration < feInfo->iterations) { USER_DATA(ctx)->dataModel.setForeach((foreach->item != NULL ? foreach->item : ""), (foreach->array != NULL ? foreach->array : ""), (foreach->index != NULL ? foreach->index : ""), feInfo->currIteration); feInfo->currIteration++; return USCXML_ERR_OK; } } catch (Event e) { execContentRaise(ctx, e.name.c_str()); free(USER_DATA(ctx)->foreachInfo[foreach]); USER_DATA(ctx)->foreachInfo.erase(foreach); return USCXML_ERR_EXEC_CONTENT; } return USCXML_ERR_FOREACH_DONE; } static int execContentForeachDone(const uscxml_ctx* ctx, const uscxml_elem_foreach* foreach) { free(USER_DATA(ctx)->foreachInfo[foreach]); USER_DATA(ctx)->foreachInfo.erase(foreach); return USCXML_ERR_OK; } static int execContentInit(const uscxml_ctx* ctx, const uscxml_elem_data* data) { while(USCXML_ELEM_DATA_IS_SET(data)) { if (USER_DATA(ctx)->invokeData.find(data->id) != USER_DATA(ctx)->invokeData.end()) { // passed via param or namelist: test245 try { USER_DATA(ctx)->dataModel.init(data->id, USER_DATA(ctx)->invokeData[data->id]); } catch (Event e) { execContentRaise(ctx, e.name.c_str()); } } else { Data d; std::stringstream content; try { if (data->expr != NULL) { d = Data(data->expr, Data::INTERPRETED); // d = USER_DATA(ctx)->dataModel.evalAsData(data->expr); } else if (data->content != NULL || data->src != NULL) { if (data->content) { content << data->content; } else { // avoid dependency on URL.cpp -> urlparser -> curl #if 0 URL sourceURL(data->src); if (USER_DATA(ctx)->baseURL.size() > 0) { sourceURL = URL::resolve(sourceURL, USER_DATA(ctx)->baseURL); } else { sourceURL = URL::resolveWithCWD(sourceURL); } content << sourceURL.getInContent(); #endif } /** * first attempt to parse as structured data, we will try * as space normalized string literals if this fails below */ d = USER_DATA(ctx)->dataModel.getAsData(content.str()); if (d.empty()) { d = Data(escape(spaceNormalize(content.str())), Data::VERBATIM); } } else { // leave d undefined } // this might fail with an unquoted string literal in content USER_DATA(ctx)->dataModel.init(data->id, d); } catch (Event e) { if (content.str().size() > 0) { try { d = Data(escape(spaceNormalize(content.str())), Data::VERBATIM); USER_DATA(ctx)->dataModel.init(data->id, d); } catch (Event e) { execContentRaise(ctx, e.name.c_str()); } } else { execContentRaise(ctx, e.name.c_str()); } } } data++; } return USCXML_ERR_OK; } static int execContentScript(const uscxml_ctx* ctx, const char* src, const char* content) { if (content != NULL) { USER_DATA(ctx)->dataModel.evalAsData(content); } else if (src != NULL) { return USCXML_ERR_UNSUPPORTED; } return USCXML_ERR_OK; } static void* dequeueExternal(const uscxml_ctx* ctx) { std::lock_guard<std::mutex> lock(USER_DATA(ctx)->mutex); if (USER_DATA(ctx)->eq.size() == 0) return NULL; // set event USER_DATA(ctx)->currEvent = USER_DATA(ctx)->eq.front(); USER_DATA(ctx)->eq.pop_front(); // get an alias const Event& e = USER_DATA(ctx)->currEvent; USER_DATA(ctx)->dataModel.setEvent(e); std::map<std::string, StateMachine*>& allMachines = USER_DATA(ctx)->topMostMachine->allMachines; if (e.invokeid.size() > 0 && allMachines.find(e.invokeid) != allMachines.end()) { // we need to check for finalize content StateMachine* invokedMachine = allMachines[e.invokeid]; if (invokedMachine->invocation != NULL && invokedMachine->invocation->finalize != NULL) invokedMachine->invocation->finalize(ctx, invokedMachine->invocation, &e); } // auto forward event for (std::map<std::string, StateMachine*>::iterator machIter = allMachines.begin(); machIter != allMachines.end(); machIter++) { if (machIter->second->parentMachine != NULL && machIter->second->parentMachine == USER_DATA(ctx) && machIter->second->invocation->autoforward) { std::lock_guard<std::mutex> lock(machIter->second->mutex); Event e2(e); machIter->second->eq.push_back(e2); } } #ifdef USCXML_VERBOSE printf("Popping External Event: %s\n", e.name.c_str()); #endif return &USER_DATA(ctx)->currEvent; } static void* dequeueInternal(const uscxml_ctx* ctx) { if (USER_DATA(ctx)->iq.size() == 0) return NULL; // set event USER_DATA(ctx)->currEvent = USER_DATA(ctx)->iq.front(); USER_DATA(ctx)->iq.pop_front(); // get an alias const Event& e = USER_DATA(ctx)->currEvent; USER_DATA(ctx)->dataModel.setEvent(e); #ifdef USCXML_VERBOSE printf("Popping Internal Event: %s\n", e.name.c_str()); #endif return &USER_DATA(ctx)->currEvent; } void eventReady(Event& e, const std::string& eventUUID) { std::lock_guard<std::mutex> lock(mutex); //std::make_tuple(e.sendid, target, type); std::string sendid = std::get<0>(sendUUIDs[e.uuid]); std::string target = std::get<1>(sendUUIDs[e.uuid]); std::string type = std::get<2>(sendUUIDs[e.uuid]); if (target == "#_internal") { e.eventType = Event::INTERNAL; #ifdef USCXML_VERBOSE printf("Pushing Internal Event: %s\n", e.name.c_str()); #endif iq.push_back(e); } else if (target == "#_external") { e.eventType = Event::EXTERNAL; #ifdef USCXML_VERBOSE printf("Pushing External Event: %s\n", e.name.c_str()); #endif eq.push_back(e); } else if (target == "#_parent") { e.eventType = Event::EXTERNAL; if (parentMachine != NULL) { parentMachine->eq.push_back(e); } // TODO: handle invalid parent } else if (target.substr(0,8) == "#_scxml_") { std::string sessionId = target.substr(8); bool sessionFound = false; for (std::map<std::string, StateMachine*>::iterator machIter = topMostMachine->allMachines.begin(); machIter != topMostMachine->allMachines.end(); machIter++) { if (machIter->second->sessionId == sessionId) { e.eventType = Event::EXTERNAL; machIter->second->eq.push_back(e); sessionFound = true; break; } } if (!sessionFound) { // test496 execContentRaise(&ctx, "error.communication"); } } else if (target.substr(0,2) == "#_") { e.eventType = Event::EXTERNAL; std::string targetId = target.substr(2); if (topMostMachine->allMachines.find(targetId) != topMostMachine->allMachines.end()) { topMostMachine->allMachines[targetId]->eq.push_back(e); } else { execContentRaise(&ctx, "error.communication"); } } else { assert(false); } monitor.notify_all(); } static std::string spaceNormalize(const std::string& text) { std::stringstream content; std::string seperator; size_t start = 0; for (size_t i = 0; i < text.size(); i++) { if (isspace(text[i])) { if (i > 0 && start < i) { content << seperator << text.substr(start, i - start); seperator = " "; } while(isspace(text[++i])); // skip whitespaces start = i; } else if (i + 1 == text.size()) { content << seperator << text.substr(start, i + 1 - start); } } return content.str(); } // TODO: isolate InterpreterImpl to reduce header deps on libxml/parser.h static bool nameMatch(const std::string& eventDescs, const std::string& eventName) { if(eventDescs.length() == 0 || eventName.length() == 0) return false; // naive case of single descriptor and exact match if (iequals(eventDescs, eventName)) return true; size_t start = 0; std::string eventDesc; for (size_t i = 0; i < eventDescs.size(); i++) { if (isspace(eventDescs[i])) { if (i > 0 && start < i - 1) { eventDesc = eventDescs.substr(start, i - start); } while(isspace(eventDescs[++i])); // skip whitespaces start = i; } else if (i + 1 == eventDescs.size()) { eventDesc = eventDescs.substr(start, i + 1 - start); } if (eventDesc.size() > 0) { // remove optional trailing .* for CCXML compatibility if (eventDesc.find("*", eventDesc.size() - 1) != std::string::npos) eventDesc = eventDesc.substr(0, eventDesc.size() - 1); if (eventDesc.find(".", eventDesc.size() - 1) != std::string::npos) eventDesc = eventDesc.substr(0, eventDesc.size() - 1); // was eventDesc the * wildcard if (eventDesc.size() == 0) return true; // eventDesc has to be a real prefix of event now and therefore shorter if (eventDesc.size() > eventName.size()) goto NEXT_DESC; // are they already equal? if (iequals(eventDesc, eventName)) return true; if (eventName.find(eventDesc) == 0) { if (eventName.find(".", eventDesc.size()) == eventDesc.size()) return true; } NEXT_DESC: eventDesc = ""; } } return false; } Event currEvent; std::map<const uscxml_elem_invoke*, std::string> invocationIds; std::map<std::string, StateMachine*> allMachines; bool isFinalized; int state; uscxml_ctx ctx; const uscxml_machine* machine; StateMachine* parentMachine; StateMachine* topMostMachine; std::map<std::string, StateMachine* >::iterator currentMachine; // next machine to advance std::string baseURL; std::string sessionId; std::string name; // in case we were invoked std::string invokeId; const uscxml_elem_invoke* invocation; std::map<std::string, Data> invokeData; std::map<const uscxml_elem_invoke*, Invoker> _invocations; std::deque<Event> iq; std::deque<Event> eq; DataModel dataModel; std::map<std::string, IOProcessor> ioProcs; std::map<std::string, Invoker> invokers; protected: struct scxml_foreach_info { size_t iterations; size_t currIteration; }; // X xmlPrefix; // XERCESC_NS::DOMDocument* document; DelayedEventQueue delayQueue; std::map<std::string, std::tuple<std::string, std::string, std::string> > sendUUIDs; std::map<const uscxml_elem_foreach*, scxml_foreach_info*> foreachInfo; std::condition_variable monitor; std::mutex mutex; }; int main(int argc, char** argv) { int err = 0; size_t benchmarkRuns = 1; const char* envBenchmarkRuns = getenv("USCXML_BENCHMARK_ITERATIONS"); if (envBenchmarkRuns != NULL) { benchmarkRuns = strTo<size_t>(envBenchmarkRuns); } // start the webserver for the basichttp tests // HTTPServer::getInstance(3453, 3454, NULL); size_t remainingRuns = benchmarkRuns; size_t microSteps = 0; #ifdef FEATS_ON_CMD Factory::getInstance()->registerDataModel(new PromelaDataModel()); #ifdef WITH_DM_ECMA_V8 Factory::getInstance()->registerDataModel(new V8DataModel()); #endif #ifdef WITH_DM_ECMA_JSC Factory::getInstance()->registerDataModel(new JSCDataModel()); #endif #ifdef WITH_DM_LUA Factory::getInstance()->registerDataModel(new LuaDataModel()); #endif #endif StateMachine rootMachine(&USCXML_MACHINE); while(remainingRuns-- > 0) { microSteps = 0; for (;;) { err = rootMachine.step(); if (rootMachine.isDone()) break; microSteps++; } microSteps++; assert(rootMachine.ctx.flags & USCXML_CTX_TOP_LEVEL_FINAL); size_t passIdx = 0; for (size_t i = 0; i < rootMachine.ctx.machine->nr_states; i++) { if (rootMachine.ctx.machine->states[i].name && strcmp(rootMachine.ctx.machine->states[i].name, "pass") == 0) { passIdx = i; break; } } if(!BIT_HAS(passIdx, rootMachine.ctx.config)) { std::cerr << "Interpreter did not end in pass" << std::endl; exit(EXIT_FAILURE); } rootMachine.reset(); } return EXIT_SUCCESS; }