Skip to content

Commit 4dfbc52

Browse files
committed
Update promise API in Thread class
1 parent a415da4 commit 4dfbc52

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

include/scratchcpp/thread.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ namespace libscratchcpp
1010

1111
class VirtualMachine;
1212
class Target;
13+
#ifdef USE_LLVM
14+
class Promise;
15+
#endif
1316
class IEngine;
1417
class Script;
1518
class ThreadPrivate;
@@ -32,8 +35,13 @@ class LIBSCRATCHCPP_EXPORT Thread
3235

3336
bool isFinished() const;
3437

38+
#ifdef USE_LLVM
39+
std::shared_ptr<Promise> promise() const;
40+
void setPromise(std::shared_ptr<Promise> promise);
41+
#else
3542
void promise();
3643
void resolvePromise();
44+
#endif
3745

3846
private:
3947
spimpl::unique_impl_ptr<ThreadPrivate> impl;

src/engine/internal/engine.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <scratchcpp/broadcast.h>
1010
#ifdef USE_LLVM
1111
#include <scratchcpp/dev/compiler.h>
12+
#include <scratchcpp/dev/promise.h>
1213
#else
1314
#include <scratchcpp/compiler.h>
1415
#endif
@@ -593,8 +594,16 @@ void Engine::step()
593594
} else {
594595
Thread *th = senderThread;
595596

596-
if (std::find_if(m_threads.begin(), m_threads.end(), [th](std::shared_ptr<Thread> thread) { return thread.get() == th; }) != m_threads.end())
597+
if (std::find_if(m_threads.begin(), m_threads.end(), [th](std::shared_ptr<Thread> thread) { return thread.get() == th; }) != m_threads.end()) {
598+
#ifdef USE_LLVM
599+
auto promise = th->promise();
600+
601+
if (promise)
602+
promise->resolve();
603+
#else
597604
th->resolvePromise();
605+
#endif
606+
}
598607

599608
resolved.push_back(broadcast);
600609
resolvedThreads.push_back(th);
@@ -2018,8 +2027,16 @@ void Engine::addBroadcastPromise(Broadcast *broadcast, Thread *sender, bool wait
20182027
// Resolve broadcast promise if it's already running
20192028
auto it = m_broadcastSenders.find(broadcast);
20202029

2021-
if (it != m_broadcastSenders.cend() && std::find_if(m_threads.begin(), m_threads.end(), [&it](std::shared_ptr<Thread> thread) { return thread.get() == it->second; }) != m_threads.end())
2030+
if (it != m_broadcastSenders.cend() && std::find_if(m_threads.begin(), m_threads.end(), [&it](std::shared_ptr<Thread> thread) { return thread.get() == it->second; }) != m_threads.end()) {
2031+
#ifdef USE_LLVM
2032+
auto promise = it->second->promise();
2033+
2034+
if (promise)
2035+
promise->resolve();
2036+
#else
20222037
it->second->resolvePromise();
2038+
#endif
2039+
}
20232040

20242041
if (wait)
20252042
m_broadcastSenders[broadcast] = sender;

src/engine/thread.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifdef USE_LLVM
66
#include <scratchcpp/script.h>
77
#include <scratchcpp/dev/executablecode.h>
8+
#include <scratchcpp/dev/executioncontext.h>
89
#endif
910

1011
#include "thread_p.h"
@@ -87,22 +88,28 @@ bool Thread::isFinished() const
8788
#endif
8889
}
8990

91+
#ifdef USE_LLVM
92+
/*! Returns the script promise. */
93+
std::shared_ptr<Promise> Thread::promise() const
94+
{
95+
return impl->executionContext->promise();
96+
}
97+
98+
/*! Sets the script promise (yields until the promise is resolved). */
99+
void Thread::setPromise(std::shared_ptr<Promise> promise)
100+
{
101+
impl->executionContext->setPromise(promise);
102+
}
103+
#else
90104
/*! Pauses the script (when it's executed using run() again) until resolvePromise() is called. */
91105
void Thread::promise()
92106
{
93-
#ifdef USE_LLVM
94-
impl->code->promise();
95-
#else
96107
impl->vm->promise();
97-
#endif
98108
}
99109

100110
/*! Resolves the promise and resumes the script. */
101111
void Thread::resolvePromise()
102112
{
103-
#ifdef USE_LLVM
104-
impl->code->resolvePromise();
105-
#else
106113
impl->vm->resolvePromise();
107-
#endif
108114
}
115+
#endif // USE_LLVM

test/thread/thread_test.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <scratchcpp/script.h>
33
#include <scratchcpp/virtualmachine.h>
44
#include <scratchcpp/dev/executioncontext.h>
5+
#include <scratchcpp/dev/promise.h>
56
#include <targetmock.h>
67
#include <enginemock.h>
78
#include <executablecodemock.h>
@@ -76,5 +77,19 @@ TEST_F(ThreadTest, IsFinished)
7677
ASSERT_TRUE(m_thread->isFinished());
7778
}
7879

79-
// TODO: Test promise() and resolvePromise()
80+
TEST_F(ThreadTest, Promise)
81+
{
82+
ASSERT_EQ(m_thread->promise(), m_ctx->promise());
83+
84+
auto promise = std::make_shared<Promise>();
85+
m_ctx->setPromise(promise);
86+
ASSERT_EQ(m_thread->promise(), m_ctx->promise());
87+
88+
m_ctx->setPromise(nullptr);
89+
ASSERT_EQ(m_thread->promise(), m_ctx->promise());
90+
91+
m_thread->setPromise(promise);
92+
ASSERT_EQ(m_thread->promise(), promise);
93+
ASSERT_EQ(m_ctx->promise(), promise);
94+
}
8095
#endif // USE_LLVM

0 commit comments

Comments
 (0)