Verified Commit d867041a authored by Jordan Petridis's avatar Jordan Petridis 🌱

matrix-api/util.rs: Make semaphore a function

parent e79bf6b8
......@@ -10,13 +10,14 @@ use util::download_file;
use util::cache_dir_path;
use util::get_room_media_list;
use util::resolve_media_url;
use util::semaphore;
use types::Message;
pub fn get_thumb_async(bk: &Backend, media: String, tx: Sender<String>) -> Result<(), Error> {
let baseu = bk.get_base_url()?;
semaphore!(bk.limit_threads, {
semaphore(bk.limit_threads.clone(), move || {
match thumb!(&baseu, &media) {
Ok(fname) => {
tx.send(fname).unwrap();
......@@ -33,7 +34,7 @@ pub fn get_thumb_async(bk: &Backend, media: String, tx: Sender<String>) -> Resul
pub fn get_media_async(bk: &Backend, media: String, tx: Sender<String>) -> Result<(), Error> {
let baseu = bk.get_base_url()?;
semaphore!(bk.limit_threads, {
semaphore(bk.limit_threads.clone(), move || {
match media!(&baseu, &media) {
Ok(fname) => {
tx.send(fname).unwrap();
......@@ -56,7 +57,7 @@ pub fn get_media_list_async(bk: &Backend,
let baseu = bk.get_base_url()?;
let tk = bk.data.lock().unwrap().access_token.clone();
semaphore!(bk.limit_threads, {
semaphore(bk.limit_threads.clone(), move || {
match get_room_media_list(&baseu, tk, roomid.clone(),
globals::PAGE_LIMIT,
first_media_id, prev_batch) {
......@@ -93,7 +94,7 @@ pub fn get_media(bk: &Backend, media: String) -> Result<(), Error> {
pub fn get_media_url(bk: &Backend, media: String, tx: Sender<String>) -> Result<(), Error> {
let baseu = bk.get_base_url()?;
semaphore!(bk.limit_threads, {
semaphore(bk.limit_threads.clone(), move || {
match resolve_media_url(&baseu, &media, false, 0, 0) {
Ok(uri) => {
tx.send(uri.to_string()).unwrap();
......
......@@ -17,6 +17,7 @@ use util::get_user_avatar;
use util::get_user_avatar_img;
use backend::types::BKResponse;
use backend::types::Backend;
use util::semaphore;
use types::Member;
use types::UserInfo;
......@@ -321,7 +322,7 @@ pub fn get_user_info_async(bk: &mut Backend,
let cache_key = u.clone();
let cache_value = info.clone();
semaphore!(bk.limit_threads, {
semaphore(bk.limit_threads.clone(), move || {
let i0 = info.lock();
match get_user_avatar(&baseu, &u) {
Ok(info) => {
......@@ -375,7 +376,7 @@ pub fn get_avatar_async(bk: &Backend, member: Option<Member>, tx: Sender<String>
let uid = m.uid.clone();
let avatar = m.avatar.clone();
semaphore!(bk.limit_threads, {
semaphore(bk.limit_threads.clone(), move || {
match get_user_avatar_img(&baseu, uid,
avatar.unwrap_or_default()) {
Ok(fname) => { tx.send(fname.clone()).unwrap(); }
......
......@@ -20,6 +20,8 @@ use std::fs::create_dir_all;
use std::io::prelude::*;
use std::collections::HashSet;
use std::sync::{Mutex, Condvar, Arc};
use std::thread;
use std::time::Duration as StdDuration;
......@@ -33,33 +35,32 @@ use self::reqwest::header::CONTENT_TYPE;
use globals;
macro_rules! semaphore {
($cv: expr, $blk: block) => {{
let thread_count = $cv.clone();
thread::spawn(move || {
// waiting, less than 20 threads at the same time
// this is a semaphore
// TODO: use std::sync::Semaphore when it's on stable version
// https://doc.rust-lang.org/1.1.0/std/sync/struct.Semaphore.html
let &(ref num, ref cvar) = &*thread_count;
{
let mut start = num.lock().unwrap();
while *start >= 20 {
start = cvar.wait(start).unwrap()
}
*start += 1;
pub fn semaphore<F>(thread_count: Arc<(Mutex<u8>, Condvar)>, func: F)
where F: FnOnce() + Send + 'static
{
thread::spawn(move || {
// waiting, less than 20 threads at the same time
// this is a semaphore
// TODO: use std::sync::Semaphore when it's on stable version
// https://doc.rust-lang.org/1.1.0/std/sync/struct.Semaphore.html
let &(ref num, ref cvar) = &*thread_count;
{
let mut start = num.lock().unwrap();
while *start >= 20 {
start = cvar.wait(start).unwrap()
}
*start += 1;
}
$blk
func();
// freeing the cvar for new threads
{
let mut counter = num.lock().unwrap();
*counter -= 1;
}
cvar.notify_one();
});
}}
// freeing the cvar for new threads
{
let mut counter = num.lock().unwrap();
*counter -= 1;
}
cvar.notify_one();
});
}
// from https://stackoverflow.com/a/43992218/1592377
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment