Skip to content

Commit

Permalink
async awaiter
Browse files Browse the repository at this point in the history
  • Loading branch information
Coldwings authored and beef9999 committed Aug 17, 2023
1 parent 6441748 commit 8b6259b
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
43 changes: 43 additions & 0 deletions common/executor/easyawaiter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2022 The Photon Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#pragma once

#include <easy/easy_io.h>
#include <easy/easy_uthread.h>
#include <photon/thread/awaiter.h>

namespace photon {

struct EasyContext {};

template <>
struct Awaiter<EasyContext> {
easy_comutex_t mtx;
Awaiter() {
easy_comutex_init(&mtx);
easy_comutex_cond_lock(&mtx);
}
~Awaiter() { easy_comutex_cond_unlock(&mtx); }
void suspend() { easy_comutex_cond_wait(&mtx); }
void resume() {
easy_comutex_cond_lock(&mtx);
easy_comutex_cond_signal(&mtx);
easy_comutex_cond_unlock(&mtx);
}
};

} // namespace photon
1 change: 1 addition & 0 deletions include/photon/common/executor/easyawaiter.h
1 change: 1 addition & 0 deletions include/photon/thread/awaiter.h
77 changes: 77 additions & 0 deletions thread/awaiter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copyright 2022 The Photon Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#pragma once

#include <errno.h>
#include <photon/thread/thread.h>

#include <atomic>
#include <future>

namespace photon {

// Special context that supports calling executor in photon thread
struct PhotonContext {};

// Context for stdthread calling executor
struct StdContext {};

// Special context that can automaticlly choose `PhotonContext` or `StdContext`
// by if photon initialized in current environment
struct AutoContext {};

template <typename T>
struct Awaiter;

template <>
struct Awaiter<PhotonContext> {
photon::semaphore sem;
Awaiter() {}
void suspend() { sem.wait(1); }
void resume() { sem.signal(1); }
};

template <>
struct Awaiter<StdContext> {
int err;
std::promise<void> p;
std::future<void> f;
Awaiter() : f(p.get_future()) {}
void suspend() { f.get(); }
void resume() { return p.set_value(); }
};

template <>
struct Awaiter<AutoContext> {
Awaiter<PhotonContext> pctx;
Awaiter<StdContext> sctx;
bool is_photon = false;
Awaiter() : is_photon(photon::CURRENT) {}
void suspend() {
if (is_photon)
pctx.suspend();
else
sctx.suspend();
}
void resume() {
if (is_photon)
pctx.resume();
else
sctx.resume();
}
};
} // namespace photon

0 comments on commit 8b6259b

Please sign in to comment.